0

I am passing an static array to jquery token input but the search results are not exact match because there is no server side query's..

like for an array ['aa','bab','aab','abb'] if I type ab I'm getting 'bab' before 'abb' and 'aab'.

can anyone help me to solve this issue..

Thank you in advance.

harishkumar329
  • 1,210
  • 4
  • 16
  • 34

2 Answers2

0

I'm not entirely sure how you wish to rank your results, at first I thought you wanted items that start with a string to be at the top, but then I don't see how 'aab' should come before 'bab' for the search query 'ab'.

Regardless, I think you have a few options, if you want to order the results based on the search query.

The first, which I would recommend, would be to build a server side script to handle this. It's a simple matter of pulling any exact matches to be the first item in the JSON array you'd need to return. This is the un-hacky way to go about it!

The second, if you decidedly wanted to keep it all client-side, would be to reorder your results array at the start of the populateDropdown method in the library (c. line 843)

Finally, and the most elegant, and 'correct' way, would be to reconfigure the onResult callback to also take the search query as a parameter, where you could then sort out the ordering of your results using the callback method.

Hope this gives some food for thought, good luck!

Chris
  • 5,882
  • 2
  • 32
  • 57
  • yes thanks.. but my question is at the very beginning stage itself I will do an ajax call to fetch all the autocomplete data's to an array, then I will do the search autocomplete with that array. Here is my problem starts, problem is I'm not able to sort like sql queries in client side. – harishkumar329 Sep 16 '13 at 07:02
  • I don't entirely understand the issue, but if you mean what I think you mean... When you put a link to a php page as the parameter of the TokenInput, that is queried every time you search in the autocomplete, not just once at the start to get the array. If you're already using an AJAX call to fetch your array from the server, AND want your results to be ordered, I don't understand why you wouldn't just write them in a php script? – Chris Sep 16 '13 at 12:02
  • No no. probably I was trying to pre-fetch the whole db locally to one js array at page load itself then using that array for search autocomplete tokeninput. thats it.. – harishkumar329 Sep 16 '13 at 12:31
  • Well glad you have it all sorted! – Chris Sep 16 '13 at 12:32
0

I have taken two search items and the match is on the first letter of each word. Limiting to 15 results. This is giving perfect results for me.

//Do the search through local data
var results = $.grep(array, function (row) {
  return row["id"].toLowerCase().indexOf(term.toLowerCase()) > -1;
});
var results1="";
if(results.length<15){
   results1 = $.grep(array, function (row) {
   return row["value"].toLowerCase().indexOf(term.toLowerCase()) > -1;
});
}
var diff = $(results1).not(results).get();
results= $.merge( $.merge([],results), diff);
Synchro
  • 35,538
  • 15
  • 81
  • 104
harishkumar329
  • 1,210
  • 4
  • 16
  • 34