Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having trouble modifying jQuery UI Autocomplete (Custom data and display). First, how to have the source option call more than one array. Second, to be able to detect which array the autocomplete result came from and use it in an if-statement.

I will try to be as clear as possible, as I am new at jQuery... any guidance would be greatly appreciated!

A more complete code example: http://codepen.io/seejaeger/pen/iqbke


Overview:

  1. Declare & concatenate 2 local arrays as the source
  2. Keep track of which array the suggestion results come from
  3. Use an if-statement to display different outputs based on their parent array

Each array item has the properties: value, label, desc

I am paying special attention to desc because it is used as non-searchable data and
provides the ability to output two different formatted results. See below:

The First Array

var funds = [
  {
    value: "fundtest",
    label: "Fund Name Test", //searchable
    desc: "(Ticker)", // non-searchable
  },
  {
    value: "fundtest2",
    label: "Fund Name Test 2", //searchable
    desc: "(Ticker)", // non-searchable
  }
];

The Second Array

var tickers = [
  {
    value: "tickertest",
    label: "(Ticker Test)", //searchable
    desc: "Fund Name", //non-searchable
  },
  {
    value: "tickertest2",
    label: "(Ticker Test)", //searchable
    desc: "Fund Name", //non-searchable
  }
];

The Output

If search suggestion comes from the funds array then, output:

item.label + " " + item.desc  

If search suggestion comes from the tickers array, then output:

item.desc + " " + item.label

Notice how they are swapped?

The different HTML output structures will allow for both results
to output to screen as: 'Fund Name (ticker)'


UPDATE

Parts 1 & 2 are now solved.
Still having some issues with part 3.
Will finally post an answer once I solve it (unless someone magically chimes in).

Big thanks to TR for his guidance.

View the latest edition of this work here:
http://codepen.io/seejaeger/pen/iqbke

share|improve this question
    
The only way to check which source is being used is to look the key up in the source. –  Bob Dalgleish Jul 10 '13 at 0:04
add comment

1 Answer

up vote 0 down vote accepted

The key factors to solving this issue were:

  • Add a key similar to origin (would be more elegant if a loop is used to append this key, I may add this in an update.)

  • Concatenating both arrays into one so that it can be read as a source

  • The if-statement within the .data funtion which weeds out which origin the autocomplete data stems from, and to output the result in a particular way based on that origin

Working demo: http://codepen.io/seejaeger/pen/iqbke

--

$(function() {

  var fund = [
    {
      value: "fundtest",
      label: "Fund Name Test", //searchable
      desc: "(ticker)", // non-searchable
      origin: "fund", // prefer to eliminate duplication
    },
    {
      value: "fundtest2",
      label: "Fund Name Test 2", //searchable
      desc: "(ticker)", // non-searchable
      origin: "fund", // prefer to eliminate duplication
    }
  ];

  var ticker = [
    {
      value: "tickertest",
      label: "(ticker test)", //searchable
      desc: "Fund Name", //non-searchable
      origin: "ticker", // prefer to eliminate duplication
    },
    {
      value: "tickertest2",
      label: "(ticker test 2)", //searchable
      desc: "Fund Name", //non-searchable
      origin: "ticker", // prefer to eliminate duplication
    }
  ];

  var suggest = fund.concat(ticker);

  $( "#project" ).autocomplete({  
    minLength: 2,  
    source: suggest, 
    focus: function( event, ui ) { 
      $( "#project" ).val( ui.item.label );
      return false;
    },
    select: function( event, ui ) {
      $( "#project" ).val( ui.item.label );
      $( "#project-id" ).val( ui.item.value );
      // $( "#project-description" ).html( ui.item.desc );
      // $( "#project-origin" ).html( ui.item.origin );

      return false;
    }
  })

 .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      if ( item.origin == 'fund') {
          return $( "<li>" )
          .append( "<a>" + item.label + " " + item.desc + "</a>" ) 
          .appendTo( ul );
      }
      else {
          return $( "<li>" )
          .append( "<a>" + item.desc + " " + item.label + "</a>" ) 
          .appendTo( ul ); 
          }
  }

});
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.