160 lines
4.4 KiB
Vue
Raw Normal View History

2025-09-17 10:19:20 +08:00
<template>
<div class="dict-input-wrapper">
<!-- 字典选择弹窗 -->
2025-10-13 17:28:33 +08:00
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="30vw" :close-on-click-modal="false" :draggable="true"
@close="handleDialogClose">
2025-09-26 17:26:06 +08:00
<el-input v-model="searchKey" placeholder="搜索(支持名称、编码、简拼)..." class="mb10" clearable @clear="filterDictData"
@input="filterDictData" />
2025-10-13 17:28:33 +08:00
<el-table :data="filteredDictData" height="300px" border @row-dblclick="selectItem" highlight-current-row>
2025-09-26 17:26:06 +08:00
<el-table-column prop="value" label="编码" align="center" width="150" />
<el-table-column prop="label" label="名称" align="center" show-overflow-tooltip />
<el-table-column prop="pinyin" label="简拼" align="center" width="100" /> <!-- 显示简拼便于调试 -->
2025-09-17 10:19:20 +08:00
</el-table>
</el-dialog>
</div>
</template>
<script setup>
import { ref, computed, watch, onMounted } from 'vue';
2025-11-03 12:12:47 +08:00
import { queryXmInfo } from '@/api/liswork/work/LisWork'
2025-10-30 18:16:57 +08:00
import { getFirstLetter } from '@/utils/pinyin.js'; // 引入拼音处理工具
2025-09-17 10:19:20 +08:00
// 组件参数
const props = defineProps({
modelValue: { type: [String, Number], default: '' },
2025-11-03 12:12:47 +08:00
dictType: { type: String },
2025-09-17 10:19:20 +08:00
placeholder: { type: String, default: '请输入或双击选择' },
clearable: { type: Boolean, default: true },
2025-09-26 17:26:06 +08:00
disabled: { type: Boolean, default: false },
dialogTitle: { type: String, default: '选择字典项' },
2025-11-03 12:12:47 +08:00
tableKey: { type: Object, default: () => { } },
2025-09-17 10:19:20 +08:00
});
// 组件事件
2025-11-03 12:12:47 +08:00
const emit = defineEmits(['select']);
2025-09-17 10:19:20 +08:00
const dictData = ref([]); // 存储带简拼的字典数据
const filteredDictData = ref([]);
const isDictLoading = ref(false);
const dialogVisible = ref(false);
const searchKey = ref('');
2025-11-03 12:12:47 +08:00
2025-09-17 10:19:20 +08:00
// 加载字典数据(并添加简拼字段)
const loadDictData = async () => {
try {
isDictLoading.value = true;
2025-11-03 12:12:47 +08:00
const requestParams = {
pageNum: 1,
pageSize: 1000, // 加载足够多的项目
yq: props.tableKey.yq, // 当前选中的仪器
};
const response = await queryXmInfo(requestParams);
const rawData = response.data.map((item, index) => {
return {
value: item.xmdh || `未知编码_${index}`, // 确保value存在
label: item.xmmc || `未知名称_${index}`, // 确保label存在
dw: item.dw || "",
refs: item.refs || "",
};
});
2025-09-17 10:19:20 +08:00
// 为每个字典项添加简拼字段
dictData.value = (rawData || []).map(item => ({
...item,
pinyin: getFirstLetter(item.label) // 新增pinyin字段存储简拼
}));
filteredDictData.value = [...dictData.value];
} catch (error) {
console.error(`加载${props.dictType}字典失败:`, error);
dictData.value = [];
filteredDictData.value = [];
} finally {
isDictLoading.value = false;
}
};
// 核心搜索逻辑(支持模糊搜索+简拼搜索)
const filterDictData = () => {
const key = searchKey.value.trim().toLowerCase();
if (!key) {
filteredDictData.value = [...dictData.value];
return;
}
filteredDictData.value = dictData.value.filter(item => {
// 1. 模糊搜索:匹配label(名称)或value(编码)
const matchLabel = item.label.toLowerCase().includes(key);
const matchValue = String(item.value).toLowerCase().includes(key);
// 2. 简拼搜索:匹配首字母简拼
const matchPinyin = item.pinyin.toLowerCase().includes(key);
// 满足任一条件即匹配
return matchLabel || matchValue || matchPinyin;
});
};
2025-11-03 12:12:47 +08:00
2025-09-17 10:19:20 +08:00
// 关键修改:打开弹窗时重新加载数据
const openDictSelector = async () => {
if (props.disabled || isDictLoading.value) {
return;
}
// 打开弹窗前先加载数据(确保每次打开都是最新的)
await loadDictData();
// 数据加载完成后再显示弹窗
dialogVisible.value = true;
searchKey.value = '';
filterDictData();
};
// 选择字典项
const selectItem = (item) => {
emit('select', item);
dialogVisible.value = false;
};
2025-11-03 12:12:47 +08:00
2025-09-17 10:19:20 +08:00
const handleDialogClose = () => {
dialogVisible.value = false;
};
2025-11-03 12:12:47 +08:00
2025-09-17 10:19:20 +08:00
defineExpose({
openDictSelector, // 暴露打开弹窗的方法
// 可选:暴露其他可能需要的方法(如刷新字典数据)
loadDictData
});
</script>
<style scoped>
/* 样式保持不变 */
.dict-input-wrapper {
position: relative;
}
.select-icon {
cursor: pointer;
color: #666;
margin-left: 5px;
}
.select-icon:hover {
color: #409eff;
}
:deep(.el-input__suffix) {
gap: 4px;
}
</style>