jquery - 为什么 Bloodhound.get() 返回未定义?

标签 jquery typeahead typeahead.js bloodhound

我尝试将下面的代码与 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-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()),该函数可以由以查询作为参数的源设置。

最佳答案

我实现了 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/

相关文章:

javascript - $.Deferrednotify()&progress()同步混淆

javascript - 如何让 Chart.JS 条形图将每个条形上的两个数据值堆叠在一起,并在每个条形上打印计算值?

jquery - 提前输入 Bloodhound POST 请求

javascript - Bloodhound:Twitter Typeahead 没有显示从远程/ajax 获取的所有结果

javascript - 删除裁剪后图像可拖动选项不起作用

javascript - 如何检查选择标签值

javascript - jquery ajax : process typeahead events in correct order?

javascript - Twitter Typeahead.js, "one of local, prefetch, or remote is required",即使提供

javascript - typeahead - minLength 触发建议列表

javascript - 无法隐式转换 System.LINQ 存在显式转换 如果您缺少强制转换