我正在尝试使用typeahead.js v0.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()设置“提前输入”的源。此函数还可用于为source设置引导-3-提前输入属性(该属性接受字符串数组或函数)。
// 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()),该函数可以由具有查询作为参数的源设置。
发布于 2014-02-06 00:57:31
我按如下方式实现了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')当你让血犬执行“a”的查询时,你不会对结果做任何事情。要指示"get()“做一些有用的事情,您需要将结果发送到输出函数。见这里的文件。
https://stackoverflow.com/questions/21591086
复制相似问题