【问题标题】:Typeahead shows results as undefinedTypeahead 将结果显示为未定义
【发布时间】:2014-10-18 16:33:34
【问题描述】:

我正在尝试使用 typeahead 来显示 google 建议。

Ajax 调用工作正常,数据返回正确:

执行前返回进程(数据); data 包含一个以“w”开头的字符串数组。

data = [“沃尔玛”,“天气”,“富国银行”,“世界明星嘻哈”, “沃尔格林”、“维基百科”、“白页”、“世界杯”、“webmd”、 “气象雷达”]

但是显示的建议显示“未定义”而不是真实的单词。 知道我在这里缺少什么吗?谢谢。

    <input type="text" class="typeahead" placeholder="Search">


    $('.typeahead').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    },
    {
        source: function (query, process) {
            $.getJSON("Home/Suggest", { query: query }, function (data) {
                return process(data);
            });
        }
    });

【问题讨论】:

    标签: javascript typeahead bootstrap-typeahead


    【解决方案1】:

    更新:

    经过一番研究,我找到了问题的答案,如果有人需要,我会在此处发布。

    诀窍是 - “进程”回调函数期望结果格式为:

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

    而不仅仅是一个字符串数组。

    $('.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);
            });
        }
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 2015-08-28
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    相关资源
    最近更新 更多