filter - 过滤树或嵌套数据
问题描述
我在“Filter on Tree or Nested Data #1562”问题之一中看到 Oli 提到过
嘿 @fr0z3nfyr 自版本 4,2 起,树子节点支持过滤 Cheers Oli :)
我找不到任何示例或代码来搜索嵌套数据。我的代码对于平面表非常有效,但对于嵌套表,它仅适用于根节点。
//data - the data for the row being filtered
//filterParams - params object passed to the filter
var match = false;
for (var key in data) {
if (data[key] != null) {
if ((data[key]).indexOf(filterParams.value) != -1) {
match = true;
}
}
}
return match;
}
function updateFilter(){
if ($("#filter-field").val() == "All Columns") {
table.setFilter(matchAny,{ value: $("#filter-value").val()});
} else {
table.setFilter($("#filter-field").val(), "like", $("#filter-value").val());
}
//var filter = $("#filter-field").val() == "All Columns" ? matchAny : $("#filter-field").val() ;
}```
Oli could you please point me to an example where Nested data filtering is supported
解决方案
我能够解决这个问题,但是通过使用过滤值重新设置表数据,并且树结构也未在过滤列表中维护。我可以通过对代码进行一些更改来维护树结构,但是一旦过滤完成,这个平面看起来更像我需要的。
// 此方法遍历 dataRows 及其子树,并调用创建过滤表数据的递归函数。
function updateFilter() {
var filtertableData = [];
table.getRows().filter(function (row) {
var rootData = row.getData();
rootData._children = [];
matchData(rootData, filtertableData);
var childRows = row.getTreeChildren();
searchForChildRows(rootData,childRows,filtertableData);
while (childRows.length != 0) {
for (var i = 0; i < childRows.length; i++) {
var childrow = childRows[i];
var childData = childrow.getData();
childData._children = [];
childRows = childrow.getTreeChildren();
searchForChildRows(childData,childRows,filtertableData);
}
}
});
table.setData(filtertableData);
}
function matchData(rootData, filtertableData, childdata) {
if (typeof childdata === "undefined") {
for (var key in rootData) {
console.log(key);
console.log(allVisibleCBSCols);
if (rootData[key] != null && typeof rootData[key] == 'string' && allVisibleCBSCols.includes(key)) {
if ((rootData[key]).indexOf($("#filter-value-Project").val()) != -1) {
filtertableData.push(rootData);
break;
}
}
}
} else {
for (var key in childdata) {
if (childdata[key] != null && typeof childdata[key] == 'string' && allVisibleCBSCols.includes(key)) {
if ((childdata[key]).indexOf($("#filter-value-Project").val()) != -1) {
//rootData._children.push(childdata);
filtertableData.push(childdata);
break;
}
}
}
}
}
function searchForChildRows(rootData,childRows,filtertableData) {
for (var i = 0; i < childRows.length; i++) {
var childrow = childRows[i];
var childData = childrow.getData();
childData._children = [];
matchData(rootData,filtertableData,childData);
}
}
推荐阅读
- java - Selenium webdriver java - 自动化 Dojo 下拉菜单 - 没有这样的元素异常
- python - ModuleNotFoundError:当我通过 virtualenv 创建新环境时,没有名为“virtualenv.seed.embed.via_app_data”的模块
- javascript - 更改列表顶部的 Django 水平滚动
- php - 如何在雄辩的关系laravel中从复选框值显示多个类别名称
- python - 如何从二维列表中删除第一列?
- regex - 为什么这个正则表达式匹配只显示捕获变量中的第一个单词,而不是整行?
- ruby-on-rails - mysql2 Ruby Gem 无法安装 ubuntu 20.04
- php - Laravel 显示简单的 500 错误(没有 Whoops 或 Ignition)
- pandas - 如何在python中将数据添加到现有的excel文件而不覆盖数据
- reactjs - 如何知道功能组件中的“子”道具组件类型?