我尝试将下面的代码与 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)}
});
更新
examples 。使用 ttAdapter() 来设置 typeahead 的来源。
此函数还可用于设置 Bootstrap-3-Typeahead 的 source
属性(接受字符串数组或函数)。 :
// 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()),该函数可以由以查询作为参数的源设置。
最佳答案
我实现了 Bloodhound.get() 如下(另请参阅此 fiddle : 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()”做一些有用的事情,您需要将结果发送到输出函数。请参阅documentation here .
关于jquery - 为什么 Bloodhound.get() 返回未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21591086/