1、需求:点击树形控件中的某个节点,需要拿到它本身和底下所有的子节点的id
1、树形控件代码
<el-tree:data="deptOptions"@node-click="getVisitCheckedNodes"ref="target_tree_Speech"node-key="id":default-expanded-keys="['00','SPACE_FIRST_SUBJECT']":highlight-current="true":filter-node-method="filterNodeIndex":check-strictly="!form.deptCheckStrictly"empty-text="暂无数据":props="defaultPropsIndex"></el-tree>
2、点击事件代码
// 点击访问权限树形控件的事件visitRightUsergetVisitCheckedNodes(node, e) {console.log('!!!', node);this.visit_id_result = node.id;this.visit_result = node.id;this.breadList = [];this.childList = [];this.nodeList = [];console.log('当前点击的节点数据', e);this.getTreeNode(e);//-------------------获取子节点数据函数this.getTreeNodeChild(e);// 把当前点击的节点的id先放入数组中this.breadList.unshift(e.data.id);this.breadList = this.unique(this.breadList);// console.log('节点数据', e);console.log('所有的子节点数据', this.unique(this.childList));this.visit_level = node.level;this.visit_name = node.name;this.get_have_visit_data_user(node.id);this.$nextTick(() => {this.$refs.treeSpeech.setCurrentKey(this.visit_id_result);});},
3、获取子节点函数
// 递归获取当前选中节点的所有子节点getTreeNodeChild(node) {console.log('当前遍历节点的数据', node);if (node.data.id) {this.childList.unshift(node.data.id);}//获取当前树节点和其子级节点if (node.childNodes.length) {for (let index = 0; index < node.childNodes.length; index++) {this.nodeList.push(node.childNodes[index]);if (node.childNodes[index].data.id) {this.childList.unshift(node.childNodes[index].data.id); //在数组头部添加元素}}}console.log('本身节点加上子节点', this.childList);console.log('下次需要遍历的节点', this.nodeList);if (this.nodeList.length) {this.nodeList.shift();console.log('777', this.nodeList[0]);if (this.nodeList[0] !== undefined) {this.getTreeNodeChild(this.nodeList[0]);}}},
效果图: