问题描述
我正在使用 https://github.com/corejavascript/typeahead.js 并努力寻找一种方法来对从 JSON 文件返回的结果进行排序,以便首先显示完全匹配。
var jobtitles = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.name);
},queryTokenizer: Bloodhound.tokenizers.whitespace,prefetch: {
cache: false,url: '/wp-content/plugins/signup/assets/json/job_titles.json',transform: function (response) {
return $.map(response,function (job) {
return {
name: job.name,id: job.id
}
});
},}
});
$('.jobtitle').typeahead({
highlight: true,hint: false,autoselect: false
},{
name: 'name',display: 'name',source: jobtitles,});
sorter: function (items) {
var beginswith = [],caseSensitive = [],caseInsensitive = [],item;
while ((item = items.shift())) {
var it = this.displayText(item);
if (!it.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item);
else if (~it.indexOf(this.query)) caseSensitive.push(item);
else caseInsensitive.push(item);
}
return beginswith.concat(caseSensitive,caseInsensitive);
},
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)