关于javascript:获取当前点击对象的所有父级数据-layui-异步加载-tree

8次阅读

共计 670 个字符,预计需要花费 2 分钟才能阅读完成。

data 数据集
pId 以后点击数据的父级 ID

利用场景

layui-tree 异步加载时 管制开合状态
上面这段代码是在 点击以后 DOM 获取数据的申请胜利函数内

obj.children = res.datas; // 异步加载的数据
function init(data,pId){var datas = data.map(function(item){
        item.spread = false; // 初始化敞开所有
        if(pId == item.id){
            item.spread = true; // 以后数据父级为 true
            if(item.pId!=0){
                // 找到上一级父级数据,再次递归(这是重点)init(parentTreeData,item.pId)
            }
        }// 留神不要加 else
        if(item.children){
            // 若一级数据没有以后点击数据,递归所有子集数据
            item.children = init(item.children,pId)
        }
        return item
    })
    return datas;
}
init(parentTreeData,obj.pId)
obj.spread = true; // 以后点击数据关上

上面展现下数据结构

[ {
    "children" : null,
    "pId" : "1002",
    "id" : "1310",
    "title" : "xxxxx"
  }, {
    "children" : null,
    "pId" : "1002",
    "id" : "1311",
    "title" : "xxxxx"
  }, {"children" : [ {} ], // 这种是有子集的
    "pId" : "1002",
    "id" : "1337",
    "title" : "xxxxx"
  } ]
正文完
 0