【问题标题】:Typeahead.js / Bloodhound display just one result [duplicate]Typeahead.js / Bloodhound 只显示一个结果 [重复]
【发布时间】:2015-08-02 22:18:57
【问题描述】:

我的 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

{
    "data": {
        "query": "los",
        "count": 4,
        "results": {
            "1": {
                "term": "Losanna"
            },
            "2": {
                "term": "Losone"
            },
            "3": {
                "term": "Lostallo"
            },
            "4": {
                "term": "Lostorf"
            }
        }
    }
}

你看出什么不对了吗?谢谢!

【问题讨论】:

标签: javascript json typeahead.js bloodhound


【解决方案1】:

这是为了确认这个问题是由这个类型的错误引起的: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 and Bloodhound showing an odd number of results 这是同一个问题,由于措辞不同,我之前找不到它

HTH。

【讨论】:

  • 我其实不必替换代码,只需在 typeahead 中添加limit:10,我的结果就开始显示了。
  • 谢谢!它确实解决了这个问题。这让我快疯了 2 个小时!
  • 这仍然是问题,所以你的代码真的很有帮助。快要发疯了:)
  • 谢谢你的回答也解决了我的问题。
  • 我在 Github 问题上尝试了@rosskevin,效果很好。 “我实际上不必替换代码,只需在 typeahead 中添加 limit:10,我的结果就开始显示了。”
猜你喜欢
  • 2015-07-19
  • 1970-01-01
  • 2014-10-15
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
相关资源
最近更新 更多