【问题标题】:Twitter's Typeahead Suggestions OrderingTwitter 的 Typeahead 建议排序
【发布时间】:2017-04-26 01:35:43
【问题描述】:

手头的问题是搜索建议没有按照图片中所示的相同起始字符排序:'ne' should be at the very top, yet it is at the very bottom

我该如何解决这个问题?

以下是我的代码`$(document).ready(function() { //var 查询 = 银行;

var queries = ['there is no need', 'need', 'no need', 'ne'];

//Dataset defined in index.php
// *****
//(NOTE: Typehead works by the order of the elements in dataset, thus
//          they are ordered in the database first based on count)

//Constructing the suggestion engine
var queries = new Bloodhound(
    {
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        local: queries
    });

// Initializing the typeahead (.typehead is the selector having what's currently
//                                  being typed)
$('.typeahead').typeahead(
    {
        hint: true,
        highlight: true, /* Enable substring highlighting */
        minLength: 1 /* Specify minimum characters required for showing result */
    },
    {
        name: 'queries',
        source: queries
    });

}); `

【问题讨论】:

    标签: search twitter typeahead bloodhound


    【解决方案1】:

    如果sorter 属性是用函数初始化的,Bloodhound 将使用它来对匹配的条目进行排序,如下所示:

    var queries = new Bloodhound(
    {
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        local: queries,
        sorter: function(a, b) {
            if (a < b) {
                return -1;
            }
            else if (a > b) {
                return 1;
            }
            else return 0;
        }
    });
    

    所以,只需扩展 Bloodhound 的初始化,就可以了。

    【讨论】:

      猜你喜欢
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多