0

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

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如果用户单击他们想要忽略的行的忽略列表单元格,我将用于将搜索结果中的行存储在忽略列表制表器中。如果将一行放在忽略列表中,我想这样做,以便该行永远不会出现在未来的搜索结果中,直到它从忽略列表中删除。

是否有内置过滤器可以帮助我完成此操作?

4

1 回答 1

2

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

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

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

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);

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

于 2018-12-04T22:39:10.187 回答