elementUieltree监听当前选中的子节点的值和对应父节点的值并转成list传到后台

mac2025-06-04  18

<el-tree ref="rootTree" show-checkbox default-expand-all :data="treeData" :props="defaultProps" node-key="menuId" :default-checked-keys="permissionId" />

js部分 具体实现需要根据需求更改参数这里只是提供了获取节点的方法

getKeys() { var aKey = this.getCheckedKeys(this.treeData, this.$refs.rootTree.getCheckedKeys(), 'menuId'); var result = []; var obj = {}; this.PermissionList=aKey //tree父节点去重 for (var i =0; i<this.PermissionList.length; i++) { if(!obj[this.PermissionList[i].permissionId]){ result.push(this.PermissionList[i]); obj[this.PermissionList[i].permissionId] = true; } } //回调 getCheckedKeys (data, keys, key) { var res = []; recursion(data, false); return res; // arr -> 树形总数据 // keys -> getCheckedKeys获取到的选中key值 // isChild -> 用来判断是否是子节点 function recursion (arr, isChild) { var aCheck = []; for ( var i = 0; i < arr.length; i++ ) { var obj = arr[i]; aCheck[i] = false; if ( obj.children ) { aCheck[i] = recursion(obj.children, true) ? true : aCheck[i]; if ( aCheck[i] ) { res.push({permissionId:obj[key]}); } } for ( var j = 0; j < keys.length; j++ ) { if ( obj[key] == keys[j] ) { aCheck[i] = true; if ( res.indexOf(obj[key]) == -1 ) { res.push({permissionId:obj[key]}); } break; } } } if ( isChild ) { return aCheck.indexOf(true) != -1; } } }
最新回复(0)