twbs / bootstrap

The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.
https://getbootstrap.com
MIT License
170.49k stars 78.84k forks source link

Update typeahead() data-source option. #1997

Closed alepee closed 12 years ago

alepee commented 12 years ago

I didn't find how to update data-source with typeahead. I tried to make it work simply with a new typehead() call and new values, but it does not update at all.

The first call works perfectly :) but the second one does.. nothing :(

$('select').change(function() {
    var id = $(this).val();
    $.getJSON(url + id, function(data) {
        $('input').typeahead({
            source: data
        });
        $('input.hide').slideDown(120);
    });
});
geuis commented 12 years ago

I ran into the same issues and finally figured a solution. When you are passing the object into typeahead(), nothing is changed if you don't also pass in duplicates of all the other properties and functions that are expected.

Luckily there's a better way.

Access the typeahead input's data attribute and modify the source array directly. E.g:

var autocomplete = $('input').typeahead(); autocomplete.data('typeahead').source = newSource; //where newSource is your own array

I just finished writing some code that will let me use remote data querying on the Typeahead module. https://gist.github.com/1848558

One issue I found is that due to how Typeahead is written, after you modify the source array the results don't get picked up till the next keystroke. In the gist above, I got around this by setting the source to [] before doing my XHR request, setting it in the callback function with the results, then triggering a keyup event on the input element again. Typeahead does technically trigger 2x, but since the initial source array is 0 length it doesn't do any lookups. I also had to add an active status that prevents an endless loop of XHR requests due to the 2x keyup events.

fat commented 12 years ago

yeah, we need to make this more extensible for sure. We'll be revisting the data source soon. Thanks for the feedback!

alepee commented 12 years ago

@geuis I tried your method by store input.typeahead() object into a variable and it works like a charm!

Thank you :)

reefdog commented 12 years ago

@fat: +1 for making this more extensible and obvious! :)

rubic0n commented 12 years ago

Wow .. I wasted 5 hours of my life before I found this post .. thanks @geuis for your solution ..

geuis commented 12 years ago

Glad you found it useful.

balaclark commented 12 years ago

It would be nice to have a better API for this, the current workaround looks a little hacky.

satgi commented 12 years ago

thank you so much,i've spent almost 2 to 3 hours working on this problem.

paulbarbu commented 12 years ago

Thank you!

beatoss commented 12 years ago

Thank you!

ralfschimmel commented 11 years ago

Nice, thanks, saved me a couple of hours there.

RusAlex commented 11 years ago

thanks guys, save my life

geuis commented 11 years ago

Is this issue still active? I thought the data source issue would have been addressed by now and my little hack made obsolete.

rodleviton commented 11 years ago

Thanks @geuis.

ScotterC commented 11 years ago

@geuis Still really useful code for eliminating duplicates even when using Typeahead's newest methods. Thank you!

jayyvis commented 11 years ago

My first failed attempt was updating the data-source using jquery. And landed up here. Thank you @geuis

greivinlopez commented 11 years ago

@geuis thank you!! You're the man! :)

zda commented 10 years ago

This doesn't actually work for me – I get TypeError: Cannot read property 'name' of undefined every time I try var autocomplete = $('input').typeahead();. $('input') itself is as expected, but and has a working typeahead in the UI, but .typeahead() still returns an error.

This happening to anyone else?

zda commented 10 years ago

PS – You can reproduce this error on the Typeahead.js website itself: http://twitter.github.io/typeahead.js/examples/

> $('.example-countries .typeahead').typeahead(); => TypeError: Cannot read property 'name' of undefined

cvrebert commented 10 years ago

@zda The typeahead widget referenced in this issue is Bootstrap v2's own old typeahead (bootstrap-typeahead.js), NOT Twitter's Typeahead.js.

zda commented 10 years ago

Yes, just realized this – thanks.

geuis commented 10 years ago

Hmm I can update this gist for 3.1 if its needed. Does the new typeahead code support remote data sources?

cvrebert commented 10 years ago

@PWKad The typeahead from Bootstrap v2 was removed. For help using the suggested replacement, Twitter's Typeahead, ask on that project's GitHub.

plwalters commented 10 years ago

@cvrebert I realized that and deleted my comment after I read through everything again my bad

jeanlyn commented 10 years ago

thanks ! it's work!