2

我通过向引导程序传递一个简单的平面 json 字符串数组(如["php","php4","php5"]. 在这种情况下,所有标准功能都可以满足我的需求。

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

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

有了这个,我想格式化我的预输入以同时使用名称和计数。也许会产生一些看起来像php5 (999)的东西,当你选择 typeahead 时,它只会输出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
        }
    });

这很好用,但我想使用 , 和 的基本版本完成繁重的工作。而且我只是通过而不是. 但是我不知道如何获得这些功能的基本版本。有没有办法做到这一点?highlightermatchersorteritem.nameitem

4

1 回答 1

1

当然,只需调用/应用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);
  }
});
于 2013-05-03T22:20:45.007 回答