I have a jquery autocomplete widget that is displaying items with custom data.
They all have a value, and description.
Currently the value is inserted into the textbox when an item is clicked and the value of an item is what is searched
What I’d like to do is have the filter use both the value and the description when filtering items to display, but keep only the value being inserted.
How can I achieve this?
If you give the autocompleter an array of objects with
valueandlabel(rather thandescription) properties, it will do the filtering for you, but only on thelabel(not thevalue).If you specify a function for the
sourceoption, you’re expected to do the filtering yourself. You receive arequestargument (an object with atermproperty) and aresponseargument (a function to call back with the results). You then return relevant results yourself, using any kind of search you like (e.g., you can search bothvalueanddescriptionin your code).E.g., loosely:
The array you return can be just strings, or it can be objects with
valueandlabelproperties. Thelabelwill be shown, thevaluewill be used when selected.So for example, here’s one using a (short) list of airports, where when you type it checks both the
value(the airport code) and thedescription(the airport name): Live copy | sourceSo typing “or” or “chic” will show you “ORD – Chicago O’Hare”, and selecting it will put “ORD” in the field. Typing “san” or “sfo” will show you “SFO – San Francisco International” and selecting it will put “SFO” in the field.