javascript - 制表符 - 基于另一个制表符的过滤

标签 javascript jquery json tabulator

我有一个包含搜索结果的制表器。这是代码:

var table = new Tabulator("#json-table", {
    layout:"fitDataFill",
    //initialFilter:[{field:"title", type:"!=", value:"ignoreList.title"}],
    ajaxURL:tabUrl,
    ajaxResponse:function(url, params, response){
        return Object.values(response);
    },
    columns:
        [{title:"Title", field:"title", align:"center"},
        {title:"Price", field:"price", align:"center"},
        {title:"Shipping Cost", field:"shippingCost.shippingServiceCost.value", align:"center"},
        {title:"Watch List", align:"center", cellClick:function(e, cell){
            var data = cell.getData();
            watchListArray.push(data);
            localStorage.setItem("watchList", JSON.stringify(watchListArray));
    },},
        {title:"Ignore List", align:"center", cellClick:function(e, cell){
            var data = cell.getData();
            ignoreListArray.push(data);
            localStorage.setItem("ignoreList", JSON.stringify(ignoreListArray));
    },
        },
        {title:"Image", align:"center"},
    ],
});

我还有另一个包含忽略列表的制表符。这是代码:

ignoreListArray = JSON.parse(localStorage.getItem("ignoreList"));

var ignoreList = new Tabulator("#json-table", {
    data:ignoreListArray,
    layout:"fitDataFill",
    columns:
        [{title:"Title", field:"title", align:"center"},
        {title:"Price", field:"price", align:"center"},
        {title:"Shipping Cost", field:"shippingCost.shippingServiceCost.value", align:"center"},
        {title:"Watch List", align:"center"},
        {title:"Ignore List", align:"center"},
        {title:"Image", align:"center"},
        ],
    });

如果用户单击他们想要忽略的行的“忽略列表”单元格,我将使用“localStorage”将搜索结果中的行存储在“忽略列表制表符”中。如果将一行放入“忽略列表”中,我希望使其在从“忽略列表”中删除之前永远不会出现在将来的搜索结果中。

是否有内置过滤可以帮助我完成此任务?

最佳答案

您需要使用自定义过滤器来实现此目的。

为了过滤掉行,您需要一种方法来将忽略列表中的行与表中的行进行比较。通常,您会为每一行分配一个 id 字段,然后检查该行的 id 是否在忽略列表中。我将在示例中使用这种方法,但您可以轻松地调整它以查找行对象中任何属性的匹配值。

在我们的自定义过滤器中,我们将使用 JavaScript 数组中内置的 filter 函数来查找数组上的任何匹配项;

function customFilter(data, filterParams){
    //data - the data for the row being filtered
    //filterParams - params object passed to the filter

    //check for matches
    var matches = ignoreListArray.filter(function(element){
        return element.id == data.id
    });

    return !matches.length; //allow row if it does not match any row in the ignore list
}

//set filter on table
table.setFilter(customFilter);

有关使用自定义过滤器的完整详细信息可以在Filter Documentation中找到

关于javascript - 制表符 - 基于另一个制表符的过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53614648/

相关文章:

JavaScript、D3条形图错误

javascript - jQuery 滚动超时

jquery - 使用 jQuery 遍历元素属性

javascript - 可拖动的音频播放器的进度 slider

javascript - 使用 nomitatim 获取 json 数据,尽管使用 mapbox 也可以

java - 使用 org/json/json/20171018 存储库读取 Java 中的 JSON 数据

javascript - NodeJS 应用程序中的 AngularJS "ReferenceError: window is not defined"

javascript - JSON 解析问题

javascript - Meteor 不加载包内的模板,为什么?

javascript - 如何在数据库中保存 Google map v3 圆形和矩形叠加层而不会出现错误?