我通过将一个简单的扁平json字符串数组(例如["php","php4","php5"])交给bootshead来工作。在这种情况下,所有标准功能都可以实现我想要的功能。

但是我想以如下格式提供数据:

[
    {
        "name":"php",
        "count":123
    },
    {
        "name":"php4",
        "count":3532
    },
    {
        "name":"php5",
        "count":999
    }
]


并且,我想格式化我的键入内容以使用名称和计数。也许产生类似于php5(999)的内容,当您选择预输入时,它将仅输出php5。我认为,只要覆盖所有功能,这将很容易做到。

$('.input-classname').typeahead({
        source: function (query, process) {
            // Source here
        },
        updater: function (item) { // What gets sent to the input box
            return item.name;
        },
        highlighter: function(item) {
            return item.name + " (" + item.count + ")";
        },
        matcher: function (item) {
            // Do the matching
        },
        sorter: function (items) {
            // Do the sorting
        }
    });


这可以正常工作,但是我想使用highlightermatchersorter的基本版本来完成繁琐的工作。我只是传递item.name而不是item。但是我不知道如何获得这些功能的基本版本。有什么办法可以做到这一点?

最佳答案

当然,只需调用/应用Typeahead原型上的方法,例如

var Typeahead = $.fn.typeahead.Constructor;

$(".input-classname").typeahead({
  // ...
  matcher: function (item) {
    // do something with item
    return Typeahead.prototype.matcher.call(this, item);
  },

  sorter: function (items) {
    // alternatively
    // do something with items
    return this.constructor.prototype.sorter.call(this, items);
  }
});

10-05 21:02
查看更多