我的前端有一個TypeAhead/Bloodhound實現,它從Play/Scala服務器獲取JSON數據。 Typeahead版本是0.11.1。實現如下:TypeAhead.js和Bloodhound顯示奇數個結果
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個結果,這應該是Typeahead/Bloodhound的默認限制。
問題是,當服務器返回5個結果時,Typeahead在疊加層中顯示0個結果。如果服務器提供4個結果,則TypeAhead在疊加層中顯示1。如果服務器提供3個結果,則TypeAhead會顯示2個結果。對於2和1的結果,它會在疊加層中顯示正確的元素數量。如果我刪除頁面長度並且服務器返回10個以上的結果,則TypeAhead會顯示5個結果(限制)。過濾器中的console.log顯示正確數量的數據結果,所以他們至少會使用Bloodhound。
這段代碼可能存在什麼問題?此TypeAhead字段是此頁面中唯一的TypeAhead字段。我檢查了DOM,並且TypeAhead產生了錯誤的結果集字段數量,所以它不是CSS的問題(也嘗試刪除所有自定義CSS)。
感謝您的答覆:)
TypeAhead的確存在一個問題,修改typeahead.bundle.js,正如Stnaire在Github頁面中所說的那樣。 – mpartan