其他分享
首页 > 其他分享> > element tree递归获取所有父级id

element tree递归获取所有父级id

作者:互联网

data里面定义三个变量

rowList: [],
parentList: [],
//最终结果
reqlist: [],    

tree节点点击事件

handleNodeClick(data, node) {
      this.getParent(node);
    //同级tree点击时置空,也可以判断
      this.reqlist = this.rowList;
      this.rowList = [];
      console.log(this.reqlist);
    },

递归代码

getParent(node) {
      var that = this;
      if (node.parent && !Array.isArray(node.parent.data)) {
        node.parent.data instanceof Object && that.rowList.push(node.data.id);
        that.getParent(node.parent);
      } else if (that.rowList.length === 0) {
        if (that.parentList.length === 0) {
          that.rowList.push(node.data.id);
        }
      } else {
        that.rowList.push(node.data.id);
      }
    },

最后reqlist就是自己包括所有父级的id

标签:node,rowList,parent,父级,tree,element,reqlist,data,id
来源: https://www.cnblogs.com/nicelife/p/15015924.html