Files
hzdyjs/web/src/components/DeptSelector/MultiDeptSelector.vue
T
2025-09-09 11:35:35 +08:00

136 lines
3.1 KiB
Vue

<template>
<el-select
v-model="selectedDepts"
placeholder="请选择关联部门"
clearable
filterable
multiple
style="width: 100%; height: 32px;"
size="default"
@change="handleChange"
@clear="handleClear"
>
<el-option
v-for="dept in deptOptions"
:key="dept.deptId"
:label="dept.deptName"
:value="dept.deptId"
/>
</el-select>
</template>
<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { listDept } from '@/api/system/dept';
import { DeptVO } from '@/api/system/dept/types';
interface DeptOption {
deptId: string | number;
deptName: string;
}
interface Props {
modelValue?: DeptOption[] | null;
}
interface Emits {
(e: 'update:modelValue', value: DeptOption[] | null): void;
(e: 'change', value: DeptOption[] | null): void;
}
const props = defineProps<Props>();
const emit = defineEmits<Emits>();
const selectedDepts = ref<(string | number)[]>([]);
const deptOptions = ref<DeptOption[]>([]);
// 获取部门列表数据
const loadDeptOptions = async () => {
try {
const response = await listDept({ parentId: 101 } as any);
if (response.data) {
// 过滤出parent_id=101的部门
const filteredDepts = response.data.filter((dept: DeptVO) => dept.parentId === 101);
deptOptions.value = filteredDepts.map((dept: DeptVO) => ({
deptId: dept.deptId,
deptName: dept.deptName
}));
}
} catch (error) {
console.error('获取部门列表失败:', error);
deptOptions.value = [];
}
};
// 处理选择变化
const handleChange = (values: (string | number)[]) => {
if (values && values.length > 0) {
const selectedOptions = values
.map(value => deptOptions.value.find(dept => dept.deptId === value))
.filter(Boolean) as DeptOption[];
emit('update:modelValue', selectedOptions);
emit('change', selectedOptions);
} else {
emit('update:modelValue', null);
emit('change', null);
}
};
// 处理清空
const handleClear = () => {
emit('update:modelValue', null);
emit('change', null);
};
// 监听外部传入的值变化
watch(
() => props.modelValue,
(newValue) => {
if (newValue && Array.isArray(newValue)) {
selectedDepts.value = newValue.map(dept => dept.deptId);
} else {
selectedDepts.value = [];
}
},
{ immediate: true }
);
onMounted(() => {
loadDeptOptions();
});
</script>
<style scoped>
/* 多选部门选择器样式 - 参考系统统一高度设置 */
:deep(.el-select) {
height: 32px !important;
}
:deep(.el-select .el-input__wrapper) {
min-height: 32px !important;
height: 32px !important;
}
/* 多选标签容器 - 参考系统设置 */
:deep(.el-select__tags) {
max-height: 28px !important;
overflow: hidden !important;
}
/* 标签样式 - 显示完整部门名称 */
:deep(.el-tag) {
height: 20px !important;
line-height: 18px !important;
margin: 4px 6px 4px 0 !important;
font-size: 12px !important;
white-space: nowrap !important;
display: inline-flex !important;
align-items: center !important;
}
/* 标签关闭按钮 */
:deep(.el-tag .el-tag__close) {
margin-left: 4px !important;
}
</style>