Typeahead.js / Bloodhound显示一个结果

11

我的Typeahead.js / Bloodhound (0.11.1)没有按预期工作。在提供的长列表json结果中,只有一些显示为建议。

例如,如果我在字段中键入los,我只得到Lostorf,没有其他任何东西,而应该显示4个可选择的项目。

这是我的代码:

HTML

<div id="remote">
<input class="typeahead" type="text">
</div>

JS

var searchablePlaces    = new Bloodhound({
    datumTokenizer      : Bloodhound.tokenizers.obj.whitespace("term"),
    queryTokenizer      : Bloodhound.tokenizers.whitespace,
    remote              : {
        url             : 'http://www.example.com/autocomplete/%QUERY/',
        wildcard        : '%QUERY',
        filter          : function(response) { return response.data.results; }
      },
    limit               : 10
});

searchablePlaces.initialize();

$('#remote .typeahead').typeahead(
{
    hint            : true,
    highlight       : true,
    minLength       : 2
},
{
    name            : 'searchable-places',
    displayKey      : "term",
    source          : searchablePlaces.ttAdapter()
})

Json

:JSON(JavaScript 对象表示法)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。JSON 是基于 JavaScript 的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。
{
    "data": {
        "query": "los",
        "count": 4,
        "results": {
            "1": {
                "term": "Losanna"
            },
            "2": {
                "term": "Losone"
            },
            "3": {
                "term": "Lostallo"
            },
            "4": {
                "term": "Lostorf"
            }
        }
    }
}

你看到有什么不对的地方吗?谢谢!


可能原因:https://github.com/twitter/typeahead.js/issues/1218 - Dr. Gianluigi Zane Zanettini
1个回答

18

确认问题是由这个typeahead bug引起的:https://github.com/twitter/typeahead.js/issues/1218

根据问题报告中joekur的建议,我通过替换以下内容来解决问题:

rendered += suggestions.length;
that._append(query, suggestions.slice(0, that.limit - rendered));

有了这个:

suggestions = suggestions.slice(0, that.limit - rendered);
rendered += suggestions.length;
that._append(query, suggestions);

我把自己的问题标记为这个问题的重复:TypeAhead.js和Bloodhound显示奇数结果。这是同样的问题,只是以前没有找到它,因为措辞不同。

希望对你有帮助。


7
我实际上不需要替换代码,只需在typeahead中添加“limit: 10”,我的结果就开始显示了。 - kross
谢谢!它解决了问题。那让我发疯了大约两个小时! - user678511
问题仍然存在,所以你的代码真的帮了很大的忙。我差点要疯掉了 :) - Jay
我尝试了Github上@rosskevin的问题,它运行良好。“实际上,我不需要替换代码,只需在typeahead中添加limit:10,我的结果就开始显示了。” - trungk18
Twitter已经放弃了这个项目,corejs-typeahead是一个维护的分支,修复了这个错误。 - Thomas W
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接