4
我無法讓它與JSON對象一起工作。我在這裏跟着多個問題,沒有任何答案對我有幫助。Twitter的typeahead +帶有JSON對象的Bloodhound
$(function() {
var items = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
prefetch: {
url: 'items.json',
filter: function(list) {
return $.map(list, function(item) {
return {
name: item.name,
category: item.category,
release: item.release,
id: item.id
};
});
}
}
});
items.initialize();
$('.typeahead').typeahead(null, {
name: 'items',
displayKey: 'name',
source: items.ttAdapter()
});
});
JSON是:
[
{"id":"4","name":"Name 1","release":"July 28, 2014","category":"Bow"},
{"id":"1","name":"Name 2","release":"October 29, 2014","category":"Bow"},
{"id":"13","name":"Name 3","release":"November 27, 2014","category":"Arrow"}
]
在prefetch.filter中使用console.log(列表)確保這些東西 – 2014-09-24 22:18:45