Tim*_*dge 2 typeahead typeahead.js bloodhound
我已经使用远程数据源实现了typeahead.js和bloodhound,并且它通常可以按预期工作。但是,我已经将typeahead的minLength设置为2,虽然我可以看到ajax请求在2个字符(和3个字符)之后触发,但是typeahead仅在4个字符或更多字符之后才提供建议。我的配置中是否缺少某些内容(我正在使用typeahead.bundle.min.js)。
var local_authorities = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify : function(datum) {
//console.log(datum);
return datum.value;
},
remote: {
url: "/addresses/autocomplete_local_authorities/%QUERY",
wildcard: '%QUERY',
filter: function (data) {
// Map the remote source JSON array to a JavaScript object array
return $.map(data.local_authorities, function (local_authority) {
return {
id: local_authority.id,
value: local_authority.name
};
});
}
}
});
$('.typeahead.local-authority').typeahead({
hint: true,
highlight: true,
minLength: 2,
}, {
limit: 8,
source: local_authorities,
displayKey: 'value',
})
.on('typeahead:selected', function (e, item) {
$('[name="local_authority_ids"').val(item.id);
});
Run Code Online (Sandbox Code Playgroud)
谢谢。
一些调试表明这是由功能引起的async(suggestions)
(1719-1727行):
function async(suggestions) {
suggestions = suggestions || [];
if (!canceled && rendered < that.limit) {
that.cancel = $.noop;
rendered += suggestions.length;
that._append(query, suggestions.slice(0, that.limit - rendered));
that.async && that.trigger("asyncReceived", query);
}
}
Run Code Online (Sandbox Code Playgroud)
该缺陷是通过设置引起rendered
到suggestions.length
调用前append
,因此,当被警犬5个recieving或多个结果的查询,suggestions.slice(0, that.limit - rendered)
总是空的,作为标准limit
是5。
仅当远程端点返回的limit
对象少于对象时才添加建议,在您的情况下,这会发生4个字母以上的情况。
由于我不是Typeahead或Bloodhound的开发人员,所以我不想(也没有时间)弄清楚为什么要像这样实现它以及如何解决这个问题,但是,一个快速的解决方法是增加limit
。
必须在第二个配置对象中设置它,例如 $( "#someId" ).typeahead( { ... }, {..., limit: 10, ...});
编辑:使用typeahead / bloodhound v.1.11.1
归档时间: |
|
查看次数: |
3731 次 |
最近记录: |