我创建了一个 Typeahead 自动完成框,并将其与 tagmanager.js 绑定(bind)在一起。一切正常,除了确保所选标签是预先输入建议列表中提供的选项之一之外。
self.tagApi = $("#itemTag").tagsManager({
onlyTagList: true, //This option is unimplemented in TagsManager 3.0
maxTags: 1
});
self.tAhead = $("#itemTag").typeahead({
name: 'items',
limit: 15,
remote: {
url: '/api/get_items_for_typeahead/%QUERY'
}
}).on('typeahead:selected', function(e, data, data_set_name) {
//I want to check that it is a valid suggestion here
$("#itemTag").tagsManager("pushTag", data.value);
});
有人知道如何获取返回的数据集吗?
最佳答案
将过滤器
添加到您的 Remote
。在其中,只需保存数据,然后在您的 selected
事件处理程序中使用即可。像这样:
$itemTag = $('#itemTag');
self.tagApi = $itemTag.tagsManager({
onlyTagList: true, // This option is unimplemented in TagsManager 3.0
maxTags: 1,
});
var lastTypeaheadResponse = null;
self.tAhead = $itemTag.typeahead({
name: 'items',
limit: 15,
remote: {
url: '/api/get_items_for_typeahead/%QUERY',
filter: function (response) {
lastTypeaheadResponse = response;
return response;
},
},
}).on('typeahead:selected', function (e, data, dataSetName) {
// iterate over lastTypeaheadResponse and check what you want to check
$itemTag.tagsManager('pushTag', data.value);
});
关于javascript - 在 Twitter Typeahead.js 中获取 Remote 返回的数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19715857/