简体   繁体   中英

Typeahead.js not showing async results

I'm working on implementing typeahead.js in a search field and I'm having problems with async results only showing once in a while, while local results always work.

My suggestion backend returns data this JSON:

[{
    "query": "anders troelsen",
    "hits": "1197",
    "queryCount": "39"
},
{
    "query": "anders fogh jensen",
    "hits": "295",
    "queryCount": "38"
}]

In the code I want to transform the above JSON into an array of strings and then show it. This is the suggester code:

    var localSuggestResults = ["anders and", "anders ladekarl", "anders høg nissen"];

    var searchSuggestions = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        local: localSuggestResults,

        remote: {
            url: '/services/suggest?prefix=%QUERY',
            wildcard: '%QUERY',

            transform: function (suggestions) {
                var suggestionArray = suggestions.map(function(suggestion) {
                    return suggestion.query
                });

                return suggestionArray;
            }
        }

    });

    $("#query").typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    },
    {
        source: searchSuggestions
    });

Any suggestions for what I'm doing wrong? Thanks a lot!

it's late for response. but the library didn't change during this time.

asyncResult works when you set {async:true} in ajax options. set source to your own function that gets the result from server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM