编辑菜单时,子菜单和按钮的上级菜单不能选择自身及其子级

pull/22/head
zhou 2022-03-24 16:56:26 +08:00
parent 5c9de05aa6
commit 301008d876
2 changed files with 25 additions and 2 deletions

14
src/utils/tree.ts Normal file
View File

@ -0,0 +1,14 @@
export function filterTree<T extends { children: T[] | undefined }>(tree: T[], iterate: (item: T, index: number) => boolean): T[] {
function filter(tree: T[]): T[] {
return tree.filter((item, index) => {
if (!iterate(item, index)) {
return false;
}
if (item.children && item.children.length > 0) {
item.children = filter(item.children);
}
return true;
})
}
return filter(tree);
}

View File

@ -10,6 +10,8 @@
import {BasicDrawer, useDrawerInner} from '/@/components/Drawer'; import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
import {list, saveOrUpdateMenu} from './menu.api'; import {list, saveOrUpdateMenu} from './menu.api';
import { useAdapt } from '/@/hooks/system/useAutoAdapt.ts'; import { useAdapt } from '/@/hooks/system/useAutoAdapt.ts';
import { filterTree } from '/@/utils/tree';
// Emits // Emits
const emit = defineEmits(['success', 'register']); const emit = defineEmits(['success', 'register']);
const {width}= useAdapt(); const {width}= useAdapt();
@ -36,8 +38,15 @@
isUpdate.value = !!data?.isUpdate; isUpdate.value = !!data?.isUpdate;
menuType.value = data?.record?.menuType; menuType.value = data?.record?.menuType;
// record id
const currentId = data?.record?.id;
// //
const treeData = await list(); let treeData = await list();
if (currentId) {
treeData = filterTree(treeData, (item: any) => item.id !== currentId);
}
updateSchema([{ updateSchema([{
field: 'parentId', field: 'parentId',
componentProps: {treeData}, componentProps: {treeData},