制表符-基于另一个制表符进行筛选



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

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

有关使用自定义过滤器的全部详细信息,请参阅过滤器文档

最新更新