vue element tree通过id找父节点、通过某个节点获取完整路径

1、通过某个节点的id,找出其所有的父节点

/**
 * 1、根据节点id,获取其所有父节点
 * @param {
   *} list 完整的树结构数组
 * @param {
   *} id 当前点击的id
 * @param {
   *} name 需要对比的id 的属性节点
 * @param {
   *} child 子节点名称
 * @returns 
 */
getAllParentArr(list,id,name,child){
   
    for(let i in list){
   
        if(list[i][name] == id){
   
            return [list[i]]
        }
        if (list[i][child]) {
   
            let node = this.getAllParentArr(list[i][child],id,name,child)
            if(!!node){
   
                return node.concat(list[i])
            }
        }
     }
 }

2、通过某个节点的id,找出他所有的子节点

/**
 * 2、通过某个节点的id,找出他所有的子节点
 * @param {
   *} list 当前点击节点的数据(不是总的树结构)
 * @param {
   *} arr 返回的数组  默认为 []
 * @returns 
 */
getChildIds(list = [], arr = []) {
   
    for (let item of list ) {
   
        arr.push(item.id);
        if (item.children && item.children.length) {
   
          this.getChildIds(item.children, arr);
        }
    }
    return arr;
},

3、通过某个节点的id,获取其父级名称(或其他)的完整路径信息

/**
 * 3、通过某个节点的id,获取其父级名称(或其他)的完整路径信息
 * @param {
   *} tree 树结构数组
 * @param {
   *} value 当前节点的id
 * @returns 
 */
getItemByIdInTree( tree,value,path=""){
   
   for(var i=0;i<tree.length;i++){
   
     let tempPath = path
     tempPath =  `${
    tempPath ? tempPath + '/' : tempPath}${
    tree[i].name}` // 避免出现在最前面的/
     if(tree[i].id == value){
   
        return tempPath
     } else if(tree[i].children){
   
         let reuslt = this.getItemByIdInTree(tree[i].children,value,tempPath)
         if(reuslt){
   
             return reuslt
         }
     }
   }
},

4、通过某个节点的id,获取对应节点的完整信息

/**
 * 4、通过某个节点的id,获取该节点的完整信息
 * @param {
   *} tree 树结构数组
 * @param {
   *} value 当前节点的id
 * @returns 
 */
getItemByIdInTree( tree,value){
   
   for(var i=0;i<tree.length;i++){
   
     let item = tree[i]
     if(tree[i].id == value){
   
        return item
     } else if(tree[i].children){
   
         let reuslt = this.getItemByIdInTree(tree[i].children,value)
         if(reuslt){
   
             return reuslt
         }
     }
   }
},

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-21 21:40:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-21 21:40:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-21 21:40:02       82 阅读
  4. Python语言-面向对象

    2023-12-21 21:40:02       91 阅读

热门阅读

  1. FFmpeg实现RTSP推流

    2023-12-21 21:40:02       65 阅读
  2. 【重点】【DP】72.编辑距离

    2023-12-21 21:40:02       65 阅读
  3. Vue框架

    2023-12-21 21:40:02       57 阅读
  4. 软件开发流程

    2023-12-21 21:40:02       50 阅读
  5. 动态规划:理解并掌握算法的艺术

    2023-12-21 21:40:02       56 阅读
  6. docker安装的php 在cli中使用

    2023-12-21 21:40:02       55 阅读
  7. uniapp整合websocket(简易版)

    2023-12-21 21:40:02       53 阅读
  8. python selenium 爬虫教程

    2023-12-21 21:40:02       64 阅读
  9. 启山智软丨机器学习的基础内容

    2023-12-21 21:40:02       70 阅读
  10. 项目架构演进过程

    2023-12-21 21:40:02       64 阅读
  11. 位运算:Leetcode371.两整数之和

    2023-12-21 21:40:02       54 阅读