From dd9f30d02bccf881b0360f7a90dfdf0c84080cb7 Mon Sep 17 00:00:00 2001 From: danshihao Date: Fri, 9 Aug 2024 17:01:39 +0800 Subject: [PATCH] =?UTF-8?q?[update=2088384]=20=E8=A7=A3=E5=86=B3=E7=88=B6?= =?UTF-8?q?=E5=AD=90=E5=85=B3=E8=81=94=E5=9B=9E=E6=98=BE=E4=BD=86=E6=98=AF?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=8B=BE=E4=B8=8A=E5=AD=90=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/system/modules/UserRoleModal.vue | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/views/system/modules/UserRoleModal.vue b/src/views/system/modules/UserRoleModal.vue index de35051..4722952 100644 --- a/src/views/system/modules/UserRoleModal.vue +++ b/src/views/system/modules/UserRoleModal.vue @@ -136,6 +136,10 @@ export default { permissionIds: that.checkedKeys.join(','), lastpermissionIds: that.defaultCheckedKeys.join(',') } + // 只勾选父节点确定后,再次打开点击父子关联时,节点只是回显,没有被真实勾上,这里需要拿到所有子集的id + if (!that.checkStrictly) { + params.permissionIds = that.collectAllRelatedKeys(this.treeData, [...that.checkedKeys]).join(',') + } that.loading = true console.log('请求参数:', params) saveRolePermission(params).then((res) => { @@ -166,6 +170,36 @@ export default { console.log(this.defaultCheckedKeys) }) }) + }, + /** + * 返回父子关联的keys + * @param tree - 树结构 + * @param keys - 当前勾选的keys + * @return {any[]} - 返回所有父子关联的key + */ + collectAllRelatedKeys (tree, keys) { + const result = new Set() // 使用集合来避免重复 + function findAndCollectNodes (nodes) { + for (const node of nodes) { + if (keys.includes(node.key)) { + collectAllDescendants(node) + } else if (node.children) { + findAndCollectNodes(node.children) + } + } + } + function collectAllDescendants (node) { + if (!result.has(node.key)) { + result.add(node.key) + } + if (node.children) { + for (const child of node.children) { + collectAllDescendants(child) + } + } + } + findAndCollectNodes(tree) + return Array.from(result) } }, watch: {