gpt4 book ai didi

javascript - 如何在 typeahead/bloodhound 中对搜索结果进行排序?

转载 作者:行者123 更新时间:2023-11-30 08:37:31 27 4
gpt4 key购买 nike

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

这是我的代码:

    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()
});

有没有人对如何实现这一目标有任何提示?

最佳答案

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

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()
});

关于javascript - 如何在 typeahead/bloodhound 中对搜索结果进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30049023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com