gpt4 book ai didi

javascript - 使用 'remote' 时,Typeahead 和 Bloodhound 会显示不相关的建议

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:16:42 25 4
gpt4 key购买 nike

当使用带有远程选项的 Typeahead/Bloodhound 时,当本地/预取结果低于“限制”(5) 时,显示的建议与输入无关。看起来它只是从结果设置的顶部开始显示,最多 5 个。

图:“爱”是预期结果,其他一切无关:

我的代码:

    var keywords = [
{"value": "Ambient"}, {"value": "Blues"},{"value": "Cinematic"},{"value": "Classical"},{"value": "Country"},
{"value": "Electronic"},{"value": "Holiday"},{"value": "Jazz"},{"value": "Lounge"},{"value": "Folk"},
{"value": "Hip Hop"},{"value": "Indie"},{"value": "Pop"},{"value": "Post Rock"},{"value": "Rock"},{"value": "Singer-Songwriter"},{"value": "Soul"},
{"value": "World"},{"value": "Happy"},{"value": "Sad"},{"value": "Love"},{"value": "Angry"},
{"value":"Joy"},{"value": "Delight"},{"value": "Light"},{"value": "Dark"},{"value": "Religious"},{"value": "Driving"},
{"value":"Excited"},{"value": "Yummy"},{"value": "Delicious"},{"value": "Fun"},{"value": "Rage"},
{"value":"Hard"},{"value": "Soft"}
];


// Instantiate the Bloodhound suggestion engine
var keywordsEngine = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: keywords,
remote: {
url: '/stub/keywords.json',
filter: function (keywords) {
// Map the remote source JSON array to a JavaScript object array
return $.map(keywords, function (keyword) {
return {
value: keyword.value
};
});
}
},
prefetch: {
url: '/stub/keywords.json',
filter: function (keywords) {
// Map the remote source JSON array to a JavaScript object array
return $.map(keywords, function (keyword) {
return {
value: keyword.value
};
});
}
}
});

// kicks off the loading/processing of `local` and `prefetch`
keywordsEngine.initialize();

$('#keyword-search-input').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'keyword',
displayKey: 'value',
// `ttAdapter` wraps the suggestion engine in an adapter that
// is compatible with the typeahead jQuery plugin
source: keywordsEngine.ttAdapter()
});

最佳答案

根据 Github Issues for Typeahead.js 上的这个帖子,经过进一步研究,我认为我需要手动过滤远程建议:

“所以我的想法是,从远程返回的数据应该已经被远程过滤了,所以不会对其进行进一步的过滤。”

https://github.com/twitter/typeahead.js/issues/148

关于javascript - 使用 'remote' 时,Typeahead 和 Bloodhound 会显示不相关的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28222914/

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