首页 > 解决方案 > 过滤树或嵌套数据

问题描述

我在“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

标签: filternestedtabulator

解决方案


我能够解决这个问题,但是通过使用过滤值重新设置表数据,并且树结构也未在过滤列表中维护。我可以通过对代码进行一些更改来维护树结构,但是一旦过滤完成,这个平面看起来更像我需要的。

// 此方法遍历 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);

        }
    }

推荐阅读