I'm trying to use typeahead.js
with bloodhound
to dynamically fetch autocomplete sugestions for an input
field while the user is typing. However, I'm a noob at it and I can't figure out how to do it properly, and I found a lot of contradicting examples online that are confusing me.
Here is as far as I got: EDIT: code updated after the suggestions in the comments:
My html:
<input type="text" data-provider="typeahead" placeholder="Cerca..." id="txtAutoSearchBox" />
My JS:
$(document).ready(function () {
//setup bloodhound engine
var suggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: './Suggestions.ashx?query=%QUERY',
wildcard: '%QUERY'
}
});
suggestions.initialize();
//assign it to input field
$('#txtAutoSearchBox').typeahead({
items: 4,
displayKey: 'value',
source: suggestions.ttAdapter()
});
});
I can confirm that the query to my server is being made correctly every time the user inputs something in the field. The server returns just a string in the following form:
[
{"value": "test adsl"},
{"value": "test"},
{"value": "testi canzoni"},
{"value": "testo incanto"},
{"value": "testimoni di geova"},
{"value": "testo siamo uguali"},
{"value": "testo straordinario chiara"},
{"value": "testo see you again"},
{"value": "testo guerriero"},
{"value": "testosterone"}
]
However, the autocomplete popup doesn't appear. How do I edit the above code to make it work properly?