我有一个.tokenInput,它对从我的网络服务返回的数据进行搜索/自动完成;这很好用。
我的问题是,无论我搜索什么,它总是从网络服务返回所有项目,而不是过滤掉那些不适合我输入的搜索字符串/标签名称的项目......
。因此,我尝试修改 onResult: 事件中返回的结果,但没有成功...
关于返回的数据:
X [对象] 的数组,其中每个对象都包含 id 和名称
还有......有趣的部分(剧本)
$("#articleTags").tokenInput("api/Article/GetClubTags", {
crossDomain: false,
prePopulate: $(this).data("pre"),
theme: "facebook",
preventDuplicates: false,
allowFreeTagging: true,
tokenValue: 'name',
onResult: function (items) {
if ($.isEmptyObject(items)) {
return [{ id: '0', name: $("tester").text() }]; // if tag not found on server (this works)..
} else {
var tagsearch = $("tester").text(); // for example "Norway".
var arrayMatch = items.forEach(function (itm) {
if (itm.name === tagsearch) {
console.log("FOUND > " + itm.name); // output works when i type in the correct string
return itm;
}
});
console.log(items);
return items; // just displaying all items, not filtering out those who does not match the string.
}
}
});
实际上...我自己想出了解决方案,并使用查询参数等进行了更多小时的测试:
解决方案是执行函数调用以获取类型化值,并用如下所示编写 api url:
function callWebServiceUrl() {
return "api/Article/GetClubTags/" + $("tester").text();
}
$("#articleTags").tokenInput(callWebServiceUrl, {
crossDomain: false,
prePopulate: $(this).data("pre"),
theme: "facebook",
preventDuplicates: false,
allowFreeTagging: true,
tokenValue: 'name',
onResult: function (item) {
if ($.isEmptyObject(item)) {
return [{ id: '0', name: $("tester").text() }];
} else {
return item;
}
},