Typeahead shows results as undefined

后端 未结 1 1295
深忆病人
深忆病人 2021-01-18 21:15

I am trying to use typeahead to display google suggestions.

The Ajax call works fine and data is returned properly:

Before executing return process(d

相关标签:
1条回答
  • 2021-01-18 21:45

    Update:

    After some research, I found an answer to my question and will post it here, if someone needs it.

    The trick is - the "process" callback function expects the results in a format:

    [{value: "string1"}, {value: "string2"}, {value: "string3"}]

    and not just an array of strings.

    $('.typeahead').typeahead(
    { hint: true, highlight: true, minLength: 1 }, // options
    {
        source: function (query, process) { // source dataset, data = array of strings
            $.getJSON('Home/Suggest', { query: query }, function (data) {
                //data=["string1", "string2", "string3"]
                //process callback function needs it 
                //in a format [{value: "string1"}, {value: "string2"}, {value: "string3"}]
                var output = $.map(data, function (string) { return { value: string }; });
                process(output);
            });
        }
    });
    
    0 讨论(0)
提交回复
热议问题