树状结构 父子节点 递归 recursion n级 树

 

树形控件 Tree - Ant Design https://ant-design.gitee.io/components/tree-cn/

 

function dig(path = '0', level = 3) {
  const list = [];
  for (let i = 0; i < 10; i += 1) {
    const key = `${path}-${i}`;
    const treeNode = {
      title: key,
      key,
    };

    if (level > 0) {
      treeNode.children = dig(key, level - 1);
    }

    list.push(treeNode);
  }
  return list;
}

  console.log(dig("0",4))

 

 

 

456
<script>
    const permissionData = [
        {
            "title": "水果",
            "key": 90101,
            "children": [
                {
                    "title": "用户管理",
                    "key": 90100,
                    "children": [
                        {
                            "title": "用户管理查看",
                            "key": 90110
                        },
                        {
                            "title": "添加",
                            "key": 90111
                        },
                        {
                            "title": "修改",
                            "key": 90112
                        },
                        {
                            "title": "删除",
                            "key": 90113
                        }
                    ]
                }
            ]
        },
        {
            "title": "蔬菜",
            "key": 1,
            "children": [
                {
                    "title": "用户管理",
                    "key": 2,
                    "children": [
                        {
                            "title": "用户管理查看",
                            "key": 3
                        },
                        {
                            "title": "添加",
                            "key": 4
                        },
                        {
                            "title": "修改",
                            "key": 5
                        },
                        {
                            "title": "删除",
                            "key": 6
                        }
                    ]
                }
            ]
        },
        {
            "title": "大米",
            "key": 7,
            "children": [
                {
                    "title": "用户管理",
                    "key": 8,
                    "children": [
                        {
                            "title": "用户管理查看",
                            "key": 9,
                            "children": [
                                {
                                    "title": "99用户管理查看",
                                    "key": 99
                                },
                                {
                                    "title": "9添加",
                                    "key": 911
                                }
                            ]
                        },
                        {
                            "title": "添加",
                            "key": 11,
                            "children": [
                                {
                                    "title": "11用户管理查看",
                                    "key": 119
                                },
                                {
                                    "title": "添加",
                                    "key": 1111
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]

    let PermissionRootMap = {};// 建立映射:所有权限对应的根权限的值

    const GetPermissionRootMap = () => {
        console.log("IN GetPermissionRootMap");
        permissionData.forEach(p1 => {
            // 注意原始数据为列表,第一次循环,可以获取rootKey;在递归时使用
            const rootKey = p1.key;
            let R = (obj) => {
                PermissionRootMap[obj.key] = rootKey;
                let children = obj.children;
                if (children) {
                    children.forEach(p2 => {
                        R(p2)
                    });
                }
            }
            R(p1);
        });
        console.log("ret ", PermissionRootMap);
    };
    GetPermissionRootMap();
</script>

  

 

 

 

 

 树形控件 Tree - Ant Design https://ant.design/components/tree-cn/

 

let PermissionRootMap = {};// 建立映射:所有权限对应的根权限的值

const GetPermissionRootMap = () => {
  console.log("IN GetPermissionRootMap");
  permissionData.forEach(p1 => {
    // 注意原始数据为列表,第一次循环,可以获取rootKey;在递归时使用
    const rootKey = p1.key;
    let R = (obj) => {
      PermissionRootMap[obj.key] = rootKey;
      let children = obj.children;
      if (children) {
        children.forEach(p2 => {
          R(p2)
        });
      }
    }
    R(p1);
  });
  console.log("ret ", PermissionRootMap);
};

 

三级商品类目树

 

posted @ 2022-02-22 16:00  papering  阅读(109)  评论(0编辑  收藏  举报