0

I am using below plugin to create autocomplete feature:

https://github.com/devbridge/jQuery-Autocomplete

below is their live demo:

http://www.devbridge.com/sourcery/components/jquery-autocomplete/#jquery-autocomplete

now the problem is, I'd like to filter the result and only show letter match start with first letter.

for example, if I typed into 'a', only countries start with 'a' should appear.

I checked this plugin's library, I saw there is a lookupFilter function there, but don't know how to use it.

would anyone who had used this library before kindly give some pointers?

I'd like to use this plugin instead of jQuery UI, as it is light weighted.

JavaScripter
  • 4,282
  • 9
  • 37
  • 45

1 Answers1

5

I checked lookupFilter in their library and below is the code:

lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
    return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
},

Here they are returning the value bascially if it is present in the results.

-1 means the letter a is not found, so they're returning the value at any occurrence.

So just tune it as:

lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
    return suggestion.value.toLowerCase().indexOf(queryLowerCase) === 0;
},

Exactly at 0th index.

This will return the results with a as first occurance.

Antti29
  • 2,953
  • 12
  • 34
  • 36
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • thanks! it works for me! Another issue is, it highlights all the matched letters, is it possible just highlight the pattern which starts from begining? I saw formatResult function is trying to RegExg all matched patterns to – JavaScripter Apr 23 '14 at 09:29
  • @Robert Definitely you can. The reason why all occurrence are highlighted because they have `'gi'` global identifier in the replace method. So you can simple remove it. – Praveen Apr 23 '14 at 09:35
  • when you say remove it, what does that mean? remove the whole function? I'd like to keep the first matched highlighted. I found if I remove this `gi`, nothing will be highlighted – JavaScripter Apr 23 '14 at 09:50