小编典典

如何有效过滤保留其现有结构的树形视图?

angularjs

我有一个树状的JSON,应该对其进行过滤,并且结果应保留该树状结构。

var tree = [
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        type: "Child",
        nodes: [
          {
            text: "Grandchild 1"
            type: "Grandchild"
          },
          {
            text: "Grandchild 2"
            type: "Grandchild"
          }
        ]
      },
      {
        text: "Child 2",
        type: "Child"
      }
    ]
  },
  {
    text: "Parent 2",
    type: "Parent"
  },
  {
    text: "Parent 3",
    type: "Parent"
  }
];

范例:

1)如果搜索查询是父级1

预期结果 :

[
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        type: "Child",
        nodes: [
          {
            text: "Grandchild 1"
            type: "Grandchild"
          },
          {
            text: "Grandchild 2"
            type: "Grandchild"
          }
        ]
      },
      {
        text: "Child 2",
        type: "Child"
      }
    ]
  }
]

2)如果搜索查询是Child 1

预期结果 :

[
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        type: "Child",
        nodes: [
          {
            text: "Grandchild 1"
            type: "Grandchild"
          },
          {
            text: "Grandchild 2"
            type: "Grandchild"
          }
        ]
      }
    ]
  }
]

3)如果搜索查询是孙子2

预期结果 :

[
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        type: "Child",
        nodes: [
          {
            text: "Grandchild 2"
            type: "Grandchild"
          }
        ]
      }
    ]
  }
]

我需要根据节点的水平(以保持树形结构 类型 在这里)。到目前为止,我已尝试递归过滤,但无法重新映射结果。

angular.module("myApp",[])
   .filter("filterTree",function(){
       return function(items,id){
          var filtered = [];
          var recursiveFilter = function(items,id){
              angular.forEach(items,function(item){
                 if(item.text.toLowerCase().indexOf(id)!=-1){
                    filtered.push(item);
                 }
                 if(angular.isArray(item.items) && item.items.length > 0){
                    recursiveFilter(item.items,id);              
                 }
              });
          };
          recursiveFilter(items,id);
          return filtered;
       }; 
    });
});

我的JSON非常大,因此基于类型的重新映射应在过滤器本身中完成。请指教。


阅读 541

收藏
2020-07-04

共1个答案

小编典典

您可以使用嵌套递归方法并过滤树,同时尊重找到的项目。

此解决方案不会更改原始数据。

function filter(array, text) {

    const getNodes = (result, object) => {

        if (object.text === text) {

            result.push(object);

            return result;

        }

        if (Array.isArray(object.nodes)) {

            const nodes = object.nodes.reduce(getNodes, []);

            if (nodes.length) result.push({ ...object, nodes });

        }

        return result;

    };



    return array.reduce(getNodes, []);

}



var tree = [{ text: "Parent 1", nodes: [{ text: "Child 1", type: "Child", nodes: [{ text: "Grandchild 1", type: "Grandchild" }, { text: "Grandchild 2", type: "Grandchild" }] }, { text: "Child 2", type: "Child" }] }, { text: "Parent 2", type: "Parent" }, { text: "Parent 3", type: "Parent" }];



console.log(filter(tree, 'Parent 1'));

console.log(filter(tree, 'Child 1'));

console.log(filter(tree, 'Grandchild 2'));


.as-console-wrapper { max-height: 100% !important; top: 0; }
2020-07-04