使用 Bloodhound / Typeahead 对结果进行精确匹配排序

问题描述

我正在使用 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,});

enter image description here

我尝试将以下代码添加到猎犬

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

但我得到错误 shift() 不是函数

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...