【发布时间】:2016-04-03 05:31:06
【问题描述】:
我的 twiter typeahead 初始化是:
var cityList = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: $('#citySearchInput').attr('data-src')+'?query=%QUERY',
wildcard: '%QUERY',
dataType: "json",
ajax : {
type: "GET"
}
}
});
$('#citySearchInput').typeahead({
minLength: 2,
highlight: true,
hint: true
}, {
display: 'value',
source: cityList,
limit:25,
valueKey: 'query',
templates: {
empty: function(data){
console.log(data);
return 'empty';
},
suggestion: function(data) {
console.log(data);
return '<p><a href="'+data.link +'">' + data.value + '</a></p>';
}
}
});
它与来自后端的答案一起工作:
[{"link":"\/en\/forecast\/Iran\/Ostan-e-Lorestan\/Defkandar","value":"Defk\u0101ndar, Ost\u0101n-e Lorest\u0101n(Iran)"},{"link":"\/en\/forecast\/Iran\/Ostan-e-Lorestan\/Defkundar","value":"Defk\u016bndar, Ost\u0101n-e Lorest\u0101n(Iran)"}]
但是当返回更多数据时,例如 8 个项目 - 总是写“空”和建议数据 0。 我在 js 中设置了从 25 到 100 的限制后修复了它,但我认为这不是正确的方法。
【问题讨论】:
标签: jquery json ajax twitter typeahead