【问题标题】:How to sort search results in typeahead/bloodhound?如何在 typeahead/bloodhound 中对搜索结果进行排序?
【发布时间】:2015-05-05 09:24:37
【问题描述】:

我正在使用 typeahead.js 0.11.1 并尝试对来自远程源的结果进行排序。根据代码应该有可能覆盖猎犬的默认排序功能。但是我的排序函数永远不会被调用。识别功能的计数相同。

这是我的代码:

    var bloodhoundSearchEngine = new Bloodhound({
    // we do not need any tokenization cause this will be done on the server
    datumTokenizer : function(d) {
        return d;
    },
    queryTokenizer : function(q) {
        return q;
    },
    sorter : function(itemA, itemB) {
        console.log(itemA);
        if (itemA.count < itemB.count) {
            return -1;
        } else if (itemA.count > itemB.count) {
            return 1;
        } else
            return 0;
    },
    identify : function(item) {
        console.log(itemA);
        return item.value;
    },
    remote : {
        url : '/suggest/?term=%term',
        wildcard : '%term',
        transform : function(response) {
            return $.map(response.suggestItems, function(item) {
                return {
                    value : item.value,
                    count : item.count
                };
            });
        },
        rateLimitBy : 'debounce',
        rateLimitWait : 300
    }
});

$('#typeaheadinput .typeahead')
        .typeahead(
                {
                    hint : true,
                    highlight : true,
                    minLength : 1
                },
                {
                    name : 'existing-names',
                    display : 'value',
                    limit : 20,
                    source : bloodhoundSearchEngine.ttAdapter()
                });

有任何关于如何实现这一点的提示吗?

【问题讨论】:

  • 你能在jsfiddle.net上创建一个演示吗?
  • 从未尝试过 jsfiddle.net,但我找到了解决问题的方法。看我的回答。

标签: javascript typeahead.js typeahead bloodhound


【解决方案1】:

排序器没有被调用,因为我使用自定义转换函数来转换来自远程服务器的建议。因此,我在我的转换函数中包含了对排序器的调用。以下代码适用于我:

var bloodhoundSearchEngine = new Bloodhound({
    // we do not need any tokenization cause this will be done on the server
    datumTokenizer : function(d) {
        return d;
    },
    queryTokenizer : function(q) {
        return q;
    },
    sorter : function(itemA, itemB) {
        console.log(itemA);
        if (itemA.count < itemB.count) {
            return -1;
        } else if (itemA.count > itemB.count) {
            return 1;
        } else
            return 0;
    },
    identify : function(item) {
        console.log(itemA);
        return item.value;
    },
    remote : {
        url : '/suggest/?term=%term',
        wildcard : '%term',
        transform : function(response) {
            return $.map(bloodhoundSearchEngine.sorter(response.suggestItems), function(item) {
                return {
                    value : item.value,
                    count : item.count
                };
            });
        },
        rateLimitBy : 'debounce',
        rateLimitWait : 300
    }
});

$('#typeaheadinput .typeahead')
        .typeahead(
                {
                    hint : true,
                    highlight : true,
                    minLength : 1
                },
                {
                    name : 'existing-names',
                    display : 'value',
                    limit : 20,
                    source : bloodhoundSearchEngine.ttAdapter()
                });

【讨论】:

    【解决方案2】:

    由于某种原因,sorted 函数也没有被我调用,多亏了René,我才能让它工作。

    var blood = new Bloodhound({
    sorter: function(a, b) {
        var input_string = $(selector).val();
        distance = Levenshtein.get(a.name, input_string) - Levenshtein.get(b.name, input_string)
        return distance;
    },
    remote: {
        transform : function(response) {
            return blood.sorter(response)
        },
    },
    

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 2017-05-14
      • 2011-07-13
      相关资源
      最近更新 更多