首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Typeahead.js /血犬只显示一个结果

Typeahead.js /血犬只显示一个结果
EN

Stack Overflow用户
提问于 2015-05-21 10:01:13
回答 1查看 5.7K关注 0票数 10

我的Typeahead.js /血犬(0.11.1)并不像预期的那样工作。在提供的json结果的长列表中,只有一些显示为建议。

例如,如果我在我的字段中键入los,当应该显示4个可选项时,我只会得到Lostorf,而不会得到任何其他内容。

这是我的密码:

代码语言:javascript
复制
<div id="remote">
<input class="typeahead" type="text">
</div>

JS

代码语言:javascript
复制
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

代码语言:javascript
复制
{
    "data": {
        "query": "los",
        "count": 4,
        "results": {
            "1": {
                "term": "Losanna"
            },
            "2": {
                "term": "Losone"
            },
            "3": {
                "term": "Lostallo"
            },
            "4": {
                "term": "Lostorf"
            }
        }
    }
}

你看到什么不对劲了吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-21 10:34:30

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

正如joekur在问题报告中所建议的那样,我解决了以下问题:

代码语言:javascript
复制
rendered += suggestions.length;
that._append(query, suggestions.slice(0, that.limit - rendered));

在这方面:

代码语言:javascript
复制
suggestions = suggestions.slice(0, that.limit - rendered);
rendered += suggestions.length;
that._append(query, suggestions);

我把我自己的问题标为重复:TypeAhead.js and Bloodhound showing an odd number of results

HTH。

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30370496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档