Bootstrap-3-Typeahead
Bootstrap-3-Typeahead copied to clipboard
Need a good example for source: function() where the data is fetch dynamically
Kudos for building this great plugin!
I think we really need a good example in the documentation for the use case where a function
is used in the source:
option to fetch data dynamically. I can imagine this being a pretty common usecase and use for bootstrap-3-typeahead.
I fiddled around for sometime until I found a good example in this SO post to figure it out.
I'm working with this at the moment:
$input.typeahead({
source: function (value, callback) {
$.getJSON($input.data('source-url'), {
q: value
}, function (data) {
callback(data.data || [])
})
},
autoSelect: true,
delay: 500,
})
The documentation explains it somewhat, but I agree that an example would be better as it took me while to grasp as well. Essentially the function can accept 1 or 2 parameters. The first parameter is the value of the input field. The second is a callback function that accepts one argument. If you are returning the data back synchronously, I believe you can just return your array from the function. If you're working asynchronously then you just pass your data to the callback variable as the only parameter.
I have this error in the browser debug console when typing in the textbox:
bootstrap-typeahead.min.js:10 Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined at b.grepper (bootstrap-typeahead.min.js:10) at b.lookup (bootstrap-typeahead.min.js:10) at b.keyup (bootstrap-typeahead.min.js:10) at HTMLInputElement.e (jquery.min.js:2) at HTMLInputElement.dispatch (jquery.min.js:3) at HTMLInputElement.q.handle (jquery.min.js:3)
eye