Bootstrap-3-Typeahead icon indicating copy to clipboard operation
Bootstrap-3-Typeahead copied to clipboard

Need a good example for source: function() where the data is fetch dynamically

Open gitanupam opened this issue 7 years ago • 3 comments

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.

gitanupam avatar May 12 '17 13:05 gitanupam

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.

thecodemill avatar Jul 05 '17 15:07 thecodemill

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)

ivanfdez avatar Jul 28 '17 06:07 ivanfdez

eye

kevilen avatar Dec 14 '17 09:12 kevilen