问题#1:
我有以下代码:
// NOTE: Initiate auto-complete
$('#edit-keyword').typeahead({
remote: '/products/autocomplete.json/%QUERY',
wildcard: '%QUERY'
}).bind('typeahead:selected', function(object, datum) {
console.log(object);
});我从服务器返回这个JSON:
1: "Bustello Cafe Coffee Regular"
110: "Barista Prima Coffeehouse Coffee Pods K Cups Darkest Roast French Roast"
713: "Bolthouse Farms Protein Plus Coffee"
5680: "Bustello Cafe Coffee Regular"
5693: "Bustello Cafe Coffee Regular"我在上面的代码片段中添加了什么来检索与每个数据相关联的ID,现在‘item...right’只返回字符串值。
问题#2:
我刚刚注意到JSON返回多个具有唯一ID的"Bustello Cafe Coffee Regular“,但呈现的下拉列表似乎一次只显示一个-我假设typeahead控件中有一个supress副本?在哪?
亚历克斯
发布于 2013-10-23 21:53:05
如果需要,您可以使用typeahead:selected和typeahead:autocompleted自定义事件来获取与每个项目相关联的ID,但是您的json需要一个适当的ID属性。例如:
[{"ID":111, "Name":"blah"},{"ID":222, "Name":"buuu"}]像这样使用jquery和typeahead:
$("#edit-keyword").on("typeahead:selected typeahead:autocompleted",
function(e, datum) {
// datum containts datum.ID here, do what you want with it
//.. i.e. assign to a hidden input or knockout observable, etc.
}
);至于你的副本的问题,我还没有看到它的配置,typeahead会尝试将你的数据转换成typeahead数据,如果它们还没有的话,所以它可能是typeahead中的_transformDatum函数来做的。
我建议您自己尝试将数据结构化为typeahead数据,以避免发生这种情况,看看会发生什么,查看https://github.com/twitter/typeahead.js#datum了解它们通常是如何结构的,在您的情况下,值将是object,您需要指定适当的valueKey属性。
https://stackoverflow.com/questions/18259175
复制相似问题