typeaheadjs& bloodhound绑定到id但显示值

时间:2014-12-18 20:12:18

标签: javascript jquery typeahead.js bloodhound

基本上我正在尝试使用Bloodhound来解析json对象的数组,但每次我选择建议时它都会显示id字段而不是name字段。

var services = [
    {"id":"1","name":"CI","created_at":"2014-12-17 21:21:36","updated_at":"2014-12-17 21:21:36"},
    {"id":"2","name":"DI","created_at":"2014-12-17 21:21:36","updated_at":"2014-12-17 21:21:36"},
    {"id":"3","name":"Message","created_at":"2014-12-17 21:21:36","updated_at":"2014-12-17 21:21:36"},
    {"id":"4","name":"Oracle","created_at":"2014-12-17 21:21:36","updated_at":"2014-12-17 21:21:36"},
    {"id":"5","name":"SAP Oracle","created_at":"2014-12-17 21:21:36","updated_at":"2014-12-17 21:21:36"},
    {"id":"6","name":"Other","created_at":"2014-12-17 21:21:36","updated_at":"2014-12-17 21:21:36"}
]; 

var Services = new Bloodhound({

datumTokenizer: function(services){
  return Bloodhound.tokenizers.whitespace(services.name)
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: services

});

Services.initialize();

$('#services').tagsinput({

typeaheadjs: {
  minLength: 1,
  highlight: true,
  name: 'services',
  displayKey: 'name',
  valueKey: 'id',
  source: Services.ttAdapter()
}
});

enter image description here

0 个答案:

没有答案