Question

I would like typeahead.js to behave like jqueryui autocomplete with regards to how it matches items. Using jqueryui autocomplete it's possible to search inside the text items. In typeahead it's only from the beginning of the string.

Autocomplete example: http://goo.gl/O43afF

Typeahead example: http://twitter.github.io/typeahead.js/examples/

With autocomplete, it seems like it's possible to assign a comparison function, but I haven't found anything like that in typeahead.

If I have a list that contains the item "Equestrian (Horses)" then I would like to get a match if I start writing "o".

Was it helpful?

Solution

Typeahead.js code as is will look for prefix matches, as you correctly say. There is a "trick" though: every datum may also contain a tokens element, which as the Typeahead documentation says is "a collection of strings that aid typeahead.js in matching datums with a given query".

The prefix matching is done against tokens. If you don't supply a tokens value for one of your datums, its value is tokenized (space-separated) for you. However, you could supply tokens to get what you want. For example, in your case you would supply a value of tokens that is all the unique substrings of all the words in your query string.

I suggest "all unique substrings of length >= 2", btw.

OTHER TIPS

typeahead's datasource is set via the 'source' parameter. So it's perfectly ok to place another method instead an array in there. Also note that it internally expects an array of strings so you have to format everything to string.

Take a look at this fiddle for an example

EDIT: this example now always generates values from Test 0 to Test 9, so you can of course only check by entering parts of "test"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top