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

posted @ 2021-07-15 15:50  nicknames  阅读(281)  评论(0编辑  收藏  举报