pawelczak / EasyAutocomplete

JQuery autocomplete plugin
http://easyautocomplete.com
MIT License
729 stars 243 forks source link

match beginning of list items #194

Open gpunktschmitz opened 8 years ago

gpunktschmitz commented 8 years ago

hi! I've got a list of street names: Braid Ave Calton Rd Grassmarket North Bridge Potterrow South Bridge

I'd like to match only the beginning of each list item. so if someone types in "br" only "braid ave" should be displayed. and if "n" is typed in only "north bridge" should be displayed and not all list items that have "n" in them.

could you please explain how this can be done?

thanks in advance!

KR Guenther

amol9supe commented 8 years ago

i have also same issue. is any solution of this ?

now my data is data: ["Amazon","CarDekho","Clovia","Coke2home","Dailyobjects","Dominos","Faballey","Flipkart","Homeshop18","IndianGiftsPortal","Indiatimes","Koovs","LensKart","Limeroad","Makemytrip","Nykaa","Pepperfry","Printland","Shopcj","Shopclues","Smiledrive","Snapdeal","Stalkbuylove","Trendin","Yepme","Zovi"],

so, when i type "S" letter then only show start with s letter.

how i can do ?

thanks.

duckzland commented 8 years ago

I have success by using something like this :

        var self = $(this), options = {
          data: self.data('autocomplete'),
          autocompleteOff: false,
          minCharNumber: 1,
          placeholder: true,
          list: {
            sort: {
              enabled: true,
              method: function(a, b) {
                return a.toLowerCase().indexOf(self.val().toLowerCase());
              }
            },
            match: {
              enabled: true
            }
          }
        };
GiacomoP commented 8 years ago

Can you please explain what 'this' is in your case? The input field? I'm getting weird results anyway :-(

pawelczak commented 8 years ago

@gpunktschmitz @amol9supe @GiacomoP If you want plugin match from the beginning of the word, use this settings for matching:

list: {
    match: {
        enabled: true,
        method:  function(element, phrase) {
            if(element.indexOf(phrase) === 0) {
                return true;
            } else {
                return false;
            }
        }
    }
}
GiacomoP commented 8 years ago

Thank you Łukasz! I was experimenting with the 'method' function as it seemed the way to do it.

dennisfrank commented 8 years ago

@pawelczak Thanks. That works nicely.

Any Idea how to adapt the highlighting accordingly?

joeljerushan commented 5 years ago

@pawelczak you should add it to documentation thats worked perfect !