3

我正在尝试将下面的代码与 typeahead.js v 0.10 一起使用

// instantiate the bloodhound suggestion engine
var numbers = new Bloodhound({
datumTokenizer: function(d) { return d; },
queryTokenizer: Bloodhound.tokenizers.whitespace,
local:  ["(A)labama","Alaska","Arizona","Arkansas"]
});

// initialize the bloodhound suggestion engine
numbers.initialize();
console.log(numbers.get('a'));

事实上,我试图解决这个问题:https ://github.com/bassjobsen/Bootstrap-3-Typeahead/issues/26 我希望如下所示的事情应该是可能的:

 $('.typeahead').typeahead(
{
items: 4,
source:function(query){return numbers.get(query)}       
});

更新

例子。用于ttAdapter()设置预先输入的来源。此函数还可用于设置Bootstrap-3-Typeaheadsource的属性(接受字符串数组或函数):

// instantiate the bloodhound suggestion engine
var numbers = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,//function(d) { return d; },
queryTokenizer: Bloodhound.tokenizers.whitespace,
local:  ["(A)labama","Alaska","Arizona","Arkansas","Arkansas2","Barkansas"]
});

// initialize the bloodhound suggestion engine
numbers.initialize();

$('.typeahead').typeahead(
{
items: 4,
source:numbers.ttAdapter()  
});

Bloodhound.js 显示:

  ttAdapter: function ttAdapter() {
                return _.bind(this.get, this);
            }

因此ttAdapter()返回一个函数 (get()),该函数可以由具有查询作为参数的源设置。

4

1 回答 1

2

我实现了 Bloodhound.get() 如下(另见这个小提琴:http: //jsfiddle.net/Fresh/HS9Wy/):

// instantiate the bloodhound suggestion engine
var numbers = new Bloodhound({
    datumTokenizer: function (d) {
        return d;
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: ["(A)labama", "Alaska", "Arizona", "Arkansas"]
});

// initialize the bloodhound suggestion engine
numbers.initialize();

// Get an array of datums which satisfy the query for 'a'
numbers.get('a', function (suggestions) {
    jQuery.each(suggestions, function (index, item) {
        console.log(item);
    });
});

您调用“get()”的问题,即

numbers.get('a')

是不是当你让 Bloodhound 执行对“a”的查询时,你没有对结果做任何事情。要指示“get()”做一些有用的事情,您需要将结果发送到输出函数。请参阅此处的文档

于 2014-02-06T00:57:31.163 回答