100 lines
2.2 KiB
Vue
100 lines
2.2 KiB
Vue
<template>
|
|
<el-select
|
|
v-model="selectedDept"
|
|
placeholder="请选择关联部门"
|
|
clearable
|
|
filterable
|
|
style="width: 100%"
|
|
@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 selectedDept = 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 = (value: string | number) => {
|
|
if (value) {
|
|
const selectedOption = deptOptions.value.find(dept => dept.deptId === value);
|
|
if (selectedOption) {
|
|
emit('update:modelValue', selectedOption);
|
|
emit('change', selectedOption);
|
|
}
|
|
} else {
|
|
emit('update:modelValue', null);
|
|
emit('change', null);
|
|
}
|
|
};
|
|
|
|
// 处理清空
|
|
const handleClear = () => {
|
|
selectedDept.value = undefined;
|
|
emit('update:modelValue', null);
|
|
emit('change', null);
|
|
};
|
|
|
|
// 监听外部传入的值变化
|
|
watch(
|
|
() => props.modelValue,
|
|
(newValue) => {
|
|
if (newValue) {
|
|
selectedDept.value = newValue.deptId;
|
|
} else {
|
|
selectedDept.value = undefined;
|
|
}
|
|
},
|
|
{ immediate: true }
|
|
);
|
|
|
|
onMounted(() => {
|
|
loadDeptOptions();
|
|
});
|
|
</script> |