【问题标题】:JQuery tokenInput (tags) : how do i filter results from webservice? It always displaying all itemsJQuery tokenInput(标签):我如何过滤来自 web 服务的结果?它总是显示所有项目
【发布时间】:2016-04-23 00:43:45
【问题描述】:

我有一个 .tokenInput,它使用从我的网络服务返回的数据进行搜索/自动完成;这很好用..

我的问题是,无论我在搜索什么,它总是返回来自网络服务的所有项目,而不是过滤掉那些不符合我输入的搜索字符串/标记名的项目...

...因此我尝试在 onResult: 事件中修改返回的结果,但没有成功...

关于返回的数据:

X [Objects] 数组,其中每个对象包含 idname

还有……有趣的部分(脚本)

$("#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.
        }
    }
});

【问题讨论】:

    标签: jquery web-services asp.net-web-api jquery-tokeninput


    【解决方案1】:

    实际上...我自己想出了解决方案,并用查询参数等进行了几个小时的测试:

    解决方案是执行一个函数调用来获取键入的值并用这样的方式组成 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;
            }
        },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      相关资源
      最近更新 更多