element tree 获取当前节点和其父级节点的数据

需求:树形菜单点击之后需要形成面包屑,所以需要当前节点和其父级节点的数据。记录一下解决方法,如下。

<el-tree
                        style="height:calc(100vh - 180px)"
                        ref="tree"
                        icon-class="el-icon-caret-right"
                        :data="dataTree"
                        :props="{label:'groupName',children:'groups'}"
                        node-key="key"
                        @node-click="TreeCk"

                        > <!-- :expand-on-click-node="false" -->
                </el-tree>
TreeCk(e){ //树节点点击
            let tree:any = this.$refs.tree;
            this.treeKey = ''; //初始化
            this.breadList = []; //初始化
            if (!e.groups){
                this.getTreeNode(tree.getNode(e.key));

                this.treeKey = e.key;
                this.treeGroupType = e.groupType;
                this.getData();

            }

        }
        getTreeNode(node){ //获取当前树节点和其父级节点
            if (node) {
                if (node.label !== undefined) {
                    this.breadList.unshift(node.label); //在数组头部添加元素
                    this.getTreeNode(node.parent); //递归
                }
            }
        }

this.breadList。可以得到当前节点和其父级的lable数组集合。

相关推荐