js返回不需要的值

Typehead.js returns unwanted value

本文关键字:不需要 返回 js      更新时间:2023-09-26

我很高兴能让整个程序正常工作,但是还有最后一个bug。当我在过滤列表上单击/悬停时,搜索字段中的值显示对象而不是所选值。

$('#searchbox').typeahead({
    limit: 10,
    minLength: 3,
    remote: {
      url: '/typeahead?s=%QUERY',
      filter: function(parsedResponse) {
            return _.map(parsedResponse, function(res){
                return {
                    value: res, /// I need this object for further functions, but I don't want this to be displayed in the searchbox....
                }
            });
      }

    },
    template: function (data) {
            return '<p>'+data.value.name+'</p>';
    }
});

根据文档,template是一个散列,而不是一个函数。更具体地说,数据集可以支持不同类型的模板。您正在寻找的是suggestion模板。

秀……你的call和options对象看起来像这样(未经测试):

$('#searchbox').typeahead({
    limit: 10,
    minLength: 3,
    remote: {
        url: '/typeahead?s=%QUERY',
        // ..filter and other stuff ..
    template: {
        // Modifies what's displayed in the suggestion list
        suggestion: function (data) {
            return '<p>'+data.value.name+'</p>';
        }
    },
    // Modifies what's displayed in the input after a suggestion is selected
    displayKey: function(data) {
         return data.value.name;
    }
});