【问题标题】:Twitter typeahead not showing results where they start with the same word [duplicate]Twitter typeahead没有显示以相同单词开头的结果[重复]
【发布时间】:2017-03-31 16:20:23
【问题描述】:

我正在使用 twitter typeahead 来提供从 webservice 返回的结果的下拉列表。我发现它只显示这些结果的一个子集;显然它禁止任何以相同的第一个单词开头的单词。

    var $termInput = $("#someId");
    var lookup = new Bloodhound({
        datumTokenizer:  Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
                url:/search/ + "%QUERY",
                wildcard: '%QUERY'
        }
    });

    lookup.initialize();

    $termInput.typeahead(
        { hint: true, highlight: true },
        {
            source: lookup,
            name: 'cases',
            displayKey: 'id',
            valueKey: 'id',
            templates: {
                empty: "<div class='omniboxresult nomatch'>No matching cases found</div>",
                suggestion: function(data) {
                    console.log(data);


                    return "<div class='omniboxresult'><div class='caseName'>" + "hi " +data.text + "</div></div>";
                }
            }
        }
    );

HTML 是:

<input type=text" class="form-control" id="someId" value="" data-provider="typeahead" autocomplete="off">

使用网络选项卡我知道我的网络服务正在返回(当我输入“欧元”时):

[{"id":"1991003933","level":0,"text":"EUROPEAN COMMUNITY"},
{"id":"1971004125","level":0,"text":"EUROPEAN ECONOMIC COMMUNITY"},
{"id":"2011007673","level":0,"text":"EUROPEAN UNION"},
{"id":"2011000582","level":0,"text":"EUROPEAN UNION"}]

但我的输出中只得到以下内容:

如果我输入“e”,我会得到更多结果,例如未被抑制的“English”(但我仍然只得到一个以“EUROPEAN”开头的结果

为什么会发生这种情况,我怎样才能让它显示它收到的所有结果?

【问题讨论】:

  • 这最终是一个预先输入的错误。请注意,有两个版本的预输入。原始的和废弃的twitter.github.io/typeahead.js 和维护的具有错误修复typeahead.js.org 的。第一个被遗弃的有这个错误,可能会永远

标签: javascript twitter-typeahead


【解决方案1】:

当我想显示名称的自动完成建议时,我在项目中遇到了类似的问题。我想显示名字的自动完成建议,然后是名字和姓氏。我的实现最终看起来像这样:

Sar = Sara, Sarah, Saralou, Sarchenko, Sargent

Sarah[space] = Sarah, Sarah Majercik, Sarah Maddux, Sarah Zarek, Sarah Anderson

不完美,因为我更喜欢有人做空间 Sarah 不再显示,但我还没有弄清楚。我的解决方案是在我的数据集前面按字母顺序添加所有唯一的名字和姓氏。我使用的是prefetch,但我认为remote 也有filter 可用的方法:

var lookup = new Bloodhound({
    datumTokenizer:  Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
            url:/search/ + "%QUERY",
            filter: function(list) {
                var uniques = [];
                // parse out unique names

                // my list was literally a list of first and last names so I concat them, you'll likely need to do a bit more work here
                return uniqes.concat(list); 
            },
            wildcard: '%QUERY'
    }
});

使用它,您的结果将如下所示:

EURO = EUROPEAN, EUROPEAN COMMUNITY, EUROPEAN ECONOMIC COMMUNITY, EUROPEAN UNION

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    相关资源
    最近更新 更多