我在我的前端有一个类型提前/血犬实现,它从Play/Scala服务器获取JSON-数据。提前输入版本为0.11.1。执行情况如下:
HTML:
<div id="typeahead" class="col-md-8">
<input class="typeahead form-control" type="text" placeholder="Select the user">
</div>JavaScript:
var engine = new Bloodhound({
datumTokenizer: function (datum) {
var fullName = fullName(datum);
return Bloodhound.tokenizers.whitespace(fullName);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify: function(obj) { return obj.id; },
remote: {
url: routes.controllers.Users.index("").url,
cache: false,
replace: function (url, query) {
if (!isEmpty(query)) {
url += encodeURIComponent(query);
}
return url;
},
filter: function (data) {
console.log(data);
return $.map(data, function (user) {
return {
id: user.id,
fullName: viewModel.fullName(user)
};
});
}
}
});
// instantiate the typeahead UI
$('#typeahead .typeahead')
.typeahead({
hint: true,
highlight: true,
minLength: 1,
},
{
name: 'engine',
displayKey: 'fullName',
source: engine
})
function fullName(data) {
if (data === undefined) return "";
else {
var fName = data.firstName === undefined ? "" : data.firstName;
var lName = data.lastName === undefined ? "" : data.lastName;
return fName + ' ' + lName;
}
};服务器给出的JSON响应:
[{"firstName":"Test","lastName":"User", "id":1}, ... ]服务器将结果分页,以便给出最多5个结果,这也被认为是类型提前/血犬的默认限制。
问题是,当服务器返回5个结果时,Typeahead会在覆盖层中显示0结果。如果服务器给出4个结果,TypeAhead会在覆盖层中显示1。如果服务器给出3个结果,TypeAhead将显示2个结果。对于2和1的结果,它显示了叠加中正确的元素数。如果删除页面长度,服务器返回超过10个结果,那么TypeAhead将显示5个结果(限制)。过滤器内的console.log显示正确的数据-结果数,因此它们至少会转到血犬。
这段代码有什么问题?此“提前”字段是本页中唯一的“预先输入”字段。我检查了DOM,TypeAhead生成了错误数量的结果集字段,所以CSS没有问题(也尝试删除所有定制的CSS )。
(谢谢你的答复:)
发布于 2015-05-08 14:24:51
发布于 2015-05-08 14:43:01
尝试使用engine.initialize()初始化engine.initialize();在filter返回suggestion对象,这在templates -> suggestion上是可用的;在source上用source:engine.ttAdapter()初始化engine;返回元素为String在suggestion,以填充“建议”下拉菜单。请参阅提前输入-示例.自定义模板
var data = [{
"firstName": "Test",
"lastName": "User",
"id": 1
}, {
"firstName": "Test2",
"lastName": "User2",
"id": 2
}, {
"firstName": "Test3",
"lastName": "User3",
"id": 3
}, {
"firstName": "Test4",
"lastName": "User4",
"id": 4
}, {
"firstName": "Test5",
"lastName": "User5",
"id": 5
}];
var engine = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: $.map(data, function(val, key) {
// return `suggestion` object for `templates` `suggestion`
return {value:val.firstName, suggestion:val}
})
});
// initialize `engine`
engine.initialize();
// instantiate the typeahead UI
$("#typeahead .typeahead")
.typeahead({
hint: true,
highlight: true,
minLength: 1,
}, {
name: "engine",
displayKey: "firstName",
templates: {
suggestion: function (data) {
// `suggestion` object passed at `engine`
console.log(data.suggestion);
// return suggestion element to display
var _suggestion = "<div>"
+ data.suggestion.firstName
+ " "
+ data.suggestion.lastName + "</div>";
return _suggestion
}
},
source: engine.ttAdapter()
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
<div id="typeahead" class="col-md-8">
<input class="typeahead form-control" type="text" placeholder="Select the user">
</div>
https://stackoverflow.com/questions/30125410
复制相似问题