mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:19:19 +08:00
fix: CompanyMenuDrawer取消勾选菜单后保存不生效的问题
问题原因:当取消勾选父菜单时,其子节点仍在checkedKeys中, ensureParentMenuIds会重新把父菜单加回来,导致取消勾选无效。 修复:在handleTreeCheckChange中,当检测到有节点被取消勾选时, 递归移除该节点的所有子节点。
This commit is contained in:
@@ -283,7 +283,41 @@ function handleSelectNone() {
|
||||
|
||||
// 处理树形控件的选中变化
|
||||
function handleTreeCheckChange(checkedKeysValue: any) {
|
||||
checkedKeys.value = checkedKeysValue;
|
||||
// 对比新旧值,找出被取消勾选的节点
|
||||
const oldKeys = new Set(checkedKeys.value);
|
||||
const newKeys = new Set(checkedKeysValue);
|
||||
|
||||
// 找出新增的(勾选)和移除的(取消勾选)的节点
|
||||
const addedKeys = newKeys.difference(oldKeys);
|
||||
const removedKeys = oldKeys.difference(newKeys);
|
||||
|
||||
// 如果是取消勾选,需要把取消节点的子节点也一并移除
|
||||
if (removedKeys.size > 0) {
|
||||
// 构建 parentId -> childIds 的映射
|
||||
const parentToChildren = new Map<number, number[]>();
|
||||
for (const menu of systemMenuList.value) {
|
||||
if (menu.parentId) {
|
||||
const children = parentToChildren.get(menu.parentId) || [];
|
||||
children.push(menu.id);
|
||||
parentToChildren.set(menu.parentId, children);
|
||||
}
|
||||
}
|
||||
|
||||
// 递归移除子节点
|
||||
function removeDescendants(nodeId: number) {
|
||||
const children = parentToChildren.get(nodeId) || [];
|
||||
for (const childId of children) {
|
||||
newKeys.delete(childId);
|
||||
removeDescendants(childId);
|
||||
}
|
||||
}
|
||||
|
||||
for (const removedId of removedKeys) {
|
||||
removeDescendants(removedId);
|
||||
}
|
||||
}
|
||||
|
||||
checkedKeys.value = [...newKeys];
|
||||
}
|
||||
|
||||
// 确保提交时包含必要的父菜单ID(递归添加所有层级的父菜单)
|
||||
|
||||
Reference in New Issue
Block a user