fix(components): 修复树选择组件再次勾选父子联动导致全选问题

This commit is contained in:
xlsea 2025-06-10 11:19:04 +08:00
parent bbda803e90
commit aeb736ebf1

View File

@ -51,7 +51,6 @@ onMounted(() => {
}
});
// watch expandAll ,optionsexpandedKeys
watch([expandAll, options], ([newVal]) => {
if (newVal) {
//
@ -82,6 +81,21 @@ function getAllMenuIds(menu: Api.System.MenuList) {
return menuIds;
}
/** 获取所有叶子节点的 ID没有子节点的节点 */
function getLeafMenuIds(menu: Api.System.MenuList): CommonType.IdType[] {
const leafIds: CommonType.IdType[] = [];
menu.forEach(item => {
if (!item.children || item.children.length === 0) {
//
leafIds.push(item.id!);
} else {
//
leafIds.push(...getLeafMenuIds(item.children));
}
});
return leafIds;
}
function handleCheckedTreeNodeAll(checked: boolean) {
if (checked) {
checkedKeys.value = getAllMenuIds(options.value);
@ -102,8 +116,15 @@ function getCheckedMenuIds(isCascade: boolean = false) {
watch(cascade, () => {
if (cascade.value) {
// ID
const allLeafIds = getLeafMenuIds(options.value);
//
const selectedLeafIds = checkedKeys.value.filter(id => allLeafIds.includes(id));
//
checkedKeys.value = selectedLeafIds;
return;
}
//
checkedKeys.value = getCheckedMenuIds(true);
});