typeahead.js icon indicating copy to clipboard operation
typeahead.js copied to clipboard

Dropdown closes and reopens on keypress with remote dataset

Open ndreckshage opened this issue 13 years ago • 32 comments

You can see this with the best pictures example here

Start typing 'broadway' (pre 1960 example that is not prefetched). At 'bro' it will be isolated from prefetched data. then each additional letter ('a', 'd', ...) will close + reopen dropdown with same value.

ndreckshage avatar Apr 05 '13 02:04 ndreckshage

Whenever the query changes, the suggestions are cleared, thus closing (well, hiding) the dropdown menu. New suggestions are then requested and when they become available, they are rendered causing the dropdown menu to appear again.

What behavior are you expecting here?

jharding avatar Apr 05 '13 03:04 jharding

To me it seems like it might be better for the dropdown to stay open, and have the results replaced. Then if there are 0/fewer results, close/resize. This way the dropdown wouldnt flash/flicker. So if you continue typing a word it would appear more natural, only eliminating other results as you specify the query

ndreckshage avatar Apr 05 '13 03:04 ndreckshage

But let's say your typeahead is backed by a dataset that only draws suggestions from a remote endpoint and let's say it takes a few seconds for this endpoint to respond. If you type bro and then quickly change the query to bas, suggestions for bro would stick around for a few seconds until the remote endpoint responded. That seems wrong to me.

I do agree that the flickering of the menu is less than ideal, I'm just not sure how to go about addressing it. Right now I think the best way to prevent flickering is to take advantage of prefetching.

jharding avatar Apr 05 '13 04:04 jharding

Prefetched is great solution, but struggles a bit with large datasets. For my purposes it works better for say, a weekly generated list of most popular terms, etc.

What about updating a localstorage key with most recent dataset and comparing new query against localstorage values. Then, leave matches, until dataset updated with server data? That way if someone switches from bro to bas, terms wouldn't match, and dropdown could be hidden. But if they do continue with broa, terms would hit and keep dropdown open.

ndreckshage avatar Apr 05 '13 04:04 ndreckshage

Hey any chance you guys figured out how to implement this? Even if not included in supported code, would like to include myself. I just want it to stay open until ajax request completes, since I'm only using remote.

kings55 avatar Apr 08 '13 13:04 kings55

I currently don't have a lot of time to work on typeahead.js so there's no way this is going to be resolved within the next week. Your best bet would be to fork typeahead.js and hack together your own solution for the time being.

jharding avatar Apr 08 '13 16:04 jharding

you can do something like

comment out !cacheHit && cb && cb(suggestions) since remote only. this will stop the empty suggestion that calls clear_suggestions

i then added a new trigger for "queryMinLength" rather than just "queryChanged" (if using minlength. so that we can run clear suggestions at certain point.)

and then for typeahead view i changed .on("queryChanged", this._clear... to on("queryMinLength and on("queryChanged whiteSpaceChanged", this._updateHint

not ideal, but it works. hope it helps

ndreckshage avatar Apr 08 '13 19:04 ndreckshage

I need this feature too.

Hi,

I am also using a Remote Source only and this feature would be great. Has there be any Decission or Discussion around there recently?

Thanks Jan

janwidmer avatar Sep 24 '13 09:09 janwidmer

@ndreckshage

Hi Ndreckshage

I facing same problem..................from last 3 days try to solve this problem

Please send me changes code

I dont clearly understand this line :

on("queryChanged", this.clear... to on("queryMinLength and on("queryChanged whiteSpaceChanged", this.updateHint

b4umel avatar Oct 16 '13 08:10 b4umel

@ndreckshage

Are you willing to share your code for the fix?

andy3rdworld avatar Nov 02 '13 23:11 andy3rdworld

Here's a quick hack for my use case (data populated from remote only) to remove flickering. Use with caution.



// old
_compareQueryToInputValue: function() {
    var inputValue = this.getInputValue(), isSameQuery = compareQueries(this.query, inputValue), isSameQueryExceptWhitespace = isSameQuery ? this.query.length !== inputValue.length : false;
    if (isSameQueryExceptWhitespace) {
        this.trigger("whitespaceChanged", {
        value: this.query
        });
    } else if (!isSameQuery) {
        this.trigger("queryChanged", {
        value: this.query = inputValue
        });
    }
},

// new
_compareQueryToInputValue: function() {
    var inputValue = this.getInputValue(), isSameQuery = compareQueries(this.query, inputValue), isSameQueryExceptWhitespace = isSameQuery ? this.query.length !== inputValue.length : false;
    if (isSameQueryExceptWhitespace) {
        this.trigger("whitespaceChanged", {
        value: this.query
        });
    } else if (!isSameQuery) {
        this.trigger("queryChanged", {
        value: this.query = inputValue
        });
        if (this.query.length == 0 || inputValue.length == 0) { // added
        this.trigger("queryZeroLength", {
            value: this.query
        });
        }
    }
},

// old
}).on("focused", this._openDropdown).on("blured", this._closeDropdown).on("blured", this._setInputValueToQuery).on("enterKeyed tabKeyed", this._handleSelection).on("queryChanged", this._clearHint).on("queryChanged", this._clearSuggestions).on("queryChanged", this._getSuggestions).on("whitespaceChanged", this._updateHint).on("queryChanged whitespaceChanged", this._openDropdown).on("queryChanged whitespaceChanged", this._setLanguageDirection).on("escKeyed", this._closeDropdown).on("escKeyed", this._setInputValueToQuery).on("tabKeyed upKeyed downKeyed", this._managePreventDefault).on("upKeyed downKeyed", this._moveDropdownCursor).on("upKeyed downKeyed", this._openDropdown).on("tabKeyed leftKeyed rightKeyed", this._autocomplete);

// new
}).on("focused", this._openDropdown).on("blured", this._closeDropdown).on("blured", this._setInputValueToQuery).on("enterKeyed tabKeyed", this._handleSelection)
                .on("queryChanged", this._clearHint)
                //.on("queryChanged", this._clearSuggestions) // removed
                .on("queryZeroLength", this._clearSuggestions) // added
                .on("queryChanged", this._getSuggestions)
                .on("whitespaceChanged", this._updateHint)
                .on("queryChanged whitespaceChanged", this._openDropdown)
                .on("queryChanged whitespaceChanged", this._setLanguageDirection)
                .on("escKeyed", this._closeDropdown).on("escKeyed", this._setInputValueToQuery).on("tabKeyed upKeyed downKeyed", this._managePreventDefault).on("upKeyed downKeyed", this._moveDropdownCursor).on("upKeyed downKeyed", this._openDropdown).on("tabKeyed leftKeyed rightKeyed", this._autocomplete);

andy3rdworld avatar Nov 03 '13 00:11 andy3rdworld

This feature would be great. I love typeahead but I had to use jquery-ui autocomplete to solve the remote data-source issue in my app. http://jsfiddle.net/handtrix/32Bck/

HaNdTriX avatar Dec 10 '13 10:12 HaNdTriX

@andy3rdworld Thanks for that bit of code. That was the easiest fix without any problems.

admin-4virtuals avatar Dec 18 '13 07:12 admin-4virtuals

I need this feature too. The flickering is annoying. Too me it is more of a problem than the bro > bas example given by @jharding. I will not implement the code changes by @andy3rdworld even though they likely will work for me. I want things to be easily updateable, etc. Great library by the way.

martijnsenden avatar Jan 07 '14 13:01 martijnsenden

Same issue here. Does anyone know how to make @andy3rdworld changes work in the most recent release? I'm somewhat knew to js.

ghost avatar Feb 04 '14 18:02 ghost

FYI, this is something I want to fix. It's the next non-trivial issue I plan on looking addressing.

jharding avatar Feb 04 '14 20:02 jharding

Forgive my lack of understanding about the internals, but it seems to me that the most recently fetched list from remote should act like the prefetched list. Each time a call is made, that new list is the new "prefetched" list and gets updated accordingly.

If I type "Mich" and ["Michael", "Michelle"] are in the list, I shouldn't need to wait for a new fetch when I type "a" to get "Michael".

Wbmstrmjb avatar Feb 13 '14 00:02 Wbmstrmjb

When typeahead.js gets remote suggestions back, they don't get added to the local data (local + prefetch). This is done to avoid inconsistencies between the Bloodhound search algorithm and the search algorithm being used by the remote server.

jharding avatar Feb 13 '14 00:02 jharding

I see. The reason why I am not the expert. :) I think we can solve our problem with a combo (load ~1000 recent ones and then have remote for "backup").

Wbmstrmjb avatar Feb 13 '14 00:02 Wbmstrmjb