【问题标题】:Get Twitter Bloodhound Typeahead suggestions without opening dropdown无需打开下拉菜单即可获取 Twitter Bloodhound Typeahead 建议
【发布时间】:2016-03-04 21:32:18
【问题描述】:

在用户输入输入并调用 typeahead:render 后,我可以使用下面的代码获得所有 Twitter Typeahead 建议。我想一直隐藏下拉菜单并仅在数组中获取建议。有没有办法实现这一点,因为 typeahead:render 可能需要打开下拉菜单。

        var bloodhoundData = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.whitespace,
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            local: localData
        });

        $('filter .typeahead').typeahead({
                hint: true,
                highlight: true,
                minLength: 1
            },
            {
                source: bloodhoundData,
                limit: 99999
            }).on('typeahead:render', getSuggestions);

        function getSuggestions() {
            var suggestions = Array.prototype.slice.call(arguments, 1);
        }

【问题讨论】:

    标签: twitter-typeahead bloodhound


    【解决方案1】:

    由于 Bloodhound.js 是一个独立的库,因此您不必对它使用 typeahead。您可以将 Bloodhound 的输入绑定到普通文本输入并检查 get 方法的结果。

    这样的事情可能会起作用,q 是输入中的文本(借用自 NFL Teams 示例):

    var myBloodhound = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      identify: function(obj) { return obj.name; },
      local: localData
    });
    
    function getMyData(q, sync) {
      myBloodhound.search(q, sync);
    }
    

    您可以查看bloodhound documentation hereexamples here

    【讨论】:

    • 感谢您的帮助!这行得通,尽管我最终只在 link 中使用了 substringMatcher,因为 Bloodhound 通常不会返回正确的结果(请参阅 link)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多