【问题标题】:Filter on Tree or Nested Data过滤树或嵌套数据
【发布时间】:2020-02-10 13:01:15
【问题描述】:

我在“Filter on Tree or Nested Data #1562”一期中看到 Oli 提到过

嘿@fr0z3nfyr 自版本 4,2 起,树子节点支持过滤 干杯 奥利:)

我找不到任何示例或代码来搜索嵌套数据。 我的代码在平面表中工作得很好,但对于嵌套表,它只适用于根节点。


            //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

【问题讨论】:

标签: filter nested tabulator


【解决方案1】:

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

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

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2023-02-11
    相关资源
    最近更新 更多