Twitter Typehead,列出对象数组
Twitter Typehead, listing object arrays
我在使用带有对象数组的 Twitter Typehead 时遇到问题。
请在此处查看 JSBin http://jsbin.com/buyudaq/edit?html,js,console,output
下面是序列化后的对象数组
var phoneNumbers =
{
"data": [
{
"phone": "+1 (923) 562-3211"
},
{
"phone": "+1 (924) 431-3216"
},
{
"phone": "+1 (932) 411-2223"
},
{
"phone": "+1 (857) 584-3623"
},
{
"phone": "+1 (914) 472-3779"
},
{
"phone": "+1 (876) 554-3637"
},
{
"phone": "+1 (916) 537-3873"
}
]
};
我已经修改了你的代码,而不是传递一个对象,传递一个数组到 'local' 选项应该可以完成工作。
由于您的源是一个对象数组,因此最好通过添加 'display' 选项来指定要显示的 属性 值。
var phones = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('phone'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify: function(obj) { return obj.phone; },
local: phoneNumbers.data });
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1 },{
name: 'phones',
source: phones,
display: 'phone' });
我在使用带有对象数组的 Twitter Typehead 时遇到问题。
请在此处查看 JSBin http://jsbin.com/buyudaq/edit?html,js,console,output
下面是序列化后的对象数组
var phoneNumbers =
{
"data": [
{
"phone": "+1 (923) 562-3211"
},
{
"phone": "+1 (924) 431-3216"
},
{
"phone": "+1 (932) 411-2223"
},
{
"phone": "+1 (857) 584-3623"
},
{
"phone": "+1 (914) 472-3779"
},
{
"phone": "+1 (876) 554-3637"
},
{
"phone": "+1 (916) 537-3873"
}
]
};
我已经修改了你的代码,而不是传递一个对象,传递一个数组到 'local' 选项应该可以完成工作。 由于您的源是一个对象数组,因此最好通过添加 'display' 选项来指定要显示的 属性 值。
var phones = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('phone'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify: function(obj) { return obj.phone; },
local: phoneNumbers.data });
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1 },{
name: 'phones',
source: phones,
display: 'phone' });