我使用 Twitter typeahead.js 0.10.5 作为建议引擎。它工作得很好,除了一个例外,我无法按照我想要的方式对建议列表进行排序。
举个例子:
var data =[{"id":1,"value":"no need"},
{"id":2,"value":"there is no need"},
{"id":3,"value":"in the need of"},
{"id":4,"value":"all I need"},
{"id":5,"value":"need"},
{"id":6,"value":"needs"},
{"id":7,"value":"all he needs"},
{"id":8,"value":"he needs"},
{"id":9,"value":"they need"},
{"id":10,"value":"you need"}]
var suggestion = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: data,
limit: 20
});
suggestion.initialize();
$('.typeahead').typeahead({
hint: true,
autoselect: true,
highlight: true,
minLength: 1
},
{
name: 'suggestion',
displayKey: 'value',
source: suggestion.ttAdapter(),
templates: {
empty: [
'<div class="empty-message">',
'no suggestion in this map',
'</div>'
].join('\n'),
suggestion: Handlebars.compile('<p><span class="suggestion-text">{{value}}</span></p>')
}
当我输入“需要”时,我确实得到了按数组中位置排序的建议,但我希望它按输入排序,这意味着顺序应该是“需要”、“需要”、“我需要的一切”......当键入“他”应该是“他需要”、“他需要的一切”、“我需要的一切”等。
我知道 Bloodhound 有一个分类器选项,但我不知道如何在这种特殊情况下使用它。