ruoyi-plus-soybean/src/components/custom/menu-tree.vue

166 lines
4.5 KiB
Vue
Raw Normal View History

2025-05-09 23:26:09 +08:00
<script setup lang="tsx">
2025-05-10 23:12:57 +08:00
import { onMounted, ref, useAttrs, watch } from 'vue';
2025-05-09 23:26:09 +08:00
import type { TreeOption, TreeSelectInst, TreeSelectProps } from 'naive-ui';
import { useBoolean } from '@sa/hooks';
2025-05-10 01:17:37 +08:00
import { fetchGetMenuTreeSelect } from '@/service/api/system';
2025-05-09 23:26:09 +08:00
import SvgIcon from '@/components/custom/svg-icon.vue';
defineOptions({ name: 'MenuTree' });
interface Props {
immediate?: boolean;
[key: string]: any;
}
const props = withDefaults(defineProps<Props>(), {
immediate: true
});
const { bool: expandAll } = useBoolean();
const { bool: checkAll } = useBoolean();
2025-05-10 01:17:37 +08:00
const expandedKeys = ref<CommonType.IdType[]>([0]);
2025-05-09 23:26:09 +08:00
const menuTreeRef = ref<TreeSelectInst | null>(null);
const value = defineModel<CommonType.IdType[]>('value', { required: false, default: [] });
const options = defineModel<Api.System.MenuList>('options', { required: false, default: [] });
2025-05-10 01:17:37 +08:00
const cascade = defineModel<boolean>('cascade', { required: false, default: true });
2025-05-09 23:26:09 +08:00
const loading = defineModel<boolean>('loading', { required: false, default: false });
const attrs: TreeSelectProps = useAttrs();
async function getMenuList() {
loading.value = true;
2025-05-10 01:17:37 +08:00
const { error, data } = await fetchGetMenuTreeSelect();
2025-05-09 23:26:09 +08:00
if (error) return;
options.value = [
{
2025-05-10 01:17:37 +08:00
id: 0,
label: '根目录',
2025-05-09 23:26:09 +08:00
icon: 'material-symbols:home-outline-rounded',
2025-05-10 01:17:37 +08:00
children: data
2025-05-09 23:26:09 +08:00
}
] as Api.System.MenuList;
2025-05-10 23:12:57 +08:00
// 折叠到只显示根节点
2025-05-09 23:26:09 +08:00
loading.value = false;
}
onMounted(() => {
if (props.immediate) {
getMenuList();
}
});
2025-05-10 23:12:57 +08:00
// 添加 watch 监听 expandAll 的变化,options有值后计算expandedKeys
2025-05-11 15:40:24 +08:00
watch([expandAll, options], ([newVal]) => {
if (newVal) {
// 展开所有节点
expandedKeys.value = getAllMenuIds(options.value);
} else {
expandedKeys.value = [0];
}
});
2025-05-10 23:12:57 +08:00
2025-05-09 23:26:09 +08:00
function renderPrefix({ option }: { option: TreeOption }) {
const renderLocalIcon = String(option.icon).startsWith('icon-');
2025-05-10 01:17:37 +08:00
let icon = renderLocalIcon ? undefined : String(option.icon ?? 'material-symbols:buttons-alt-outline-rounded');
2025-05-09 23:26:09 +08:00
const localIcon = renderLocalIcon ? String(option.icon).replace('icon-', 'menu-') : undefined;
2025-05-10 01:17:37 +08:00
if (icon === '#') {
icon = 'material-symbols:buttons-alt-outline-rounded';
}
2025-05-09 23:26:09 +08:00
return <SvgIcon icon={icon} localIcon={localIcon} />;
}
function getAllMenuIds(menu: Api.System.MenuList) {
const menuIds: CommonType.IdType[] = [];
menu.forEach(item => {
2025-05-10 23:12:57 +08:00
menuIds.push(item.id!);
2025-05-09 23:26:09 +08:00
if (item.children) {
menuIds.push(...getAllMenuIds(item.children));
}
});
return menuIds;
}
function handleCheckedTreeNodeAll(checked: boolean) {
if (checked) {
value.value = getAllMenuIds(options.value);
return;
}
value.value = [];
}
function handleSubmit() {
const menuIds = [...value.value];
const indeterminateData = menuTreeRef.value?.getIndeterminateData();
if (cascade.value) {
const parentIds: string[] = indeterminateData?.keys.filter(item => !menuIds?.includes(String(item))) as string[];
menuIds?.push(...parentIds);
}
return menuIds;
}
defineExpose({
submit: handleSubmit,
refresh: getMenuList
});
</script>
<template>
<div class="w-full flex-col gap-12px">
<div class="w-full flex-center">
<NCheckbox v-model:checked="expandAll" :checked-value="true" :unchecked-value="false">展开/折叠</NCheckbox>
<NCheckbox
v-model:checked="checkAll"
:checked-value="true"
:unchecked-value="false"
@update:checked="handleCheckedTreeNodeAll"
>
全选/反选
</NCheckbox>
<NCheckbox v-model:checked="cascade" :checked-value="true" :unchecked-value="false">父子联动</NCheckbox>
</div>
<NSpin class="resource h-full w-full py-6px pl-3px" content-class="h-full" :show="loading">
<NTree
ref="menuTreeRef"
v-model:checked-keys="value"
2025-05-10 01:17:37 +08:00
v-model:expanded-keys="expandedKeys"
2025-05-09 23:26:09 +08:00
multiple
checkable
2025-05-10 01:17:37 +08:00
:selectable="false"
key-field="id"
label-field="label"
2025-05-09 23:26:09 +08:00
:data="options"
:cascade="cascade"
:loading="loading"
virtual-scroll
2025-05-10 01:17:37 +08:00
check-strategy="all"
2025-05-09 23:26:09 +08:00
:render-prefix="renderPrefix"
v-bind="attrs"
/>
</NSpin>
</div>
</template>
<style scoped lang="scss">
.resource {
border-radius: 6px;
border: 1px solid rgb(224, 224, 230);
.n-tree {
min-height: 200px;
max-height: 300px;
width: 100%;
height: 100%;
:deep(.n-tree__empty) {
min-height: 200px;
justify-content: center;
}
}
.n-empty {
justify-content: center;
}
}
</style>