我使用以下代码,我基于示例https://twitter.github.io/typeahead.js/examples/:
var carArray = [{ carId: 1, fullName: 'Opel' }, { carId: 2, fullName: 'BMW' }, { carId: 3, fullName: 'Mercedes' }];
var cars = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('fullName'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
//local: carArray,
remote: '/api/cars'
});
cars.initialize();
$(elt).typeahead(null, {
name: 'car',
displayKey: 'fullName',
source: cars.ttAdapter()
});
取消注释local
它可以正常工作,但是remote
列表不会被过滤,我会收到所有项目。
所以这是remote
:
[{"carId":1,"optionOne":"ASD","optionTwo":"QEW","fullName":"Opel"}, {"carId":2,"optionOne":"FAS","optionTwo":"QEW","fullName":"BMW"},{"carId":3,"optionOne":"ADD","optionTwo":"QEW","fullName":"Mercedes"}]
谢谢