Fix Bug #576: AI修复
This commit is contained in:
@@ -45,30 +45,40 @@ const visible = ref(false);
|
||||
const unselectedItems = ref([]);
|
||||
const selectedItems = ref([]);
|
||||
|
||||
const fetchItems = async () => {
|
||||
// 修复 Bug #576:监听弹窗显示状态与编辑数据,确保回显逻辑在正确时机触发
|
||||
watch([visible, () => props.editData], async ([isVisible, data]) => {
|
||||
if (isVisible) {
|
||||
await fetchItems(data);
|
||||
}
|
||||
});
|
||||
|
||||
const fetchItems = async (editData = null) => {
|
||||
try {
|
||||
const res = await getLabCatalogItems();
|
||||
const allItems = res.data || [];
|
||||
unselectedItems.value = [...allItems];
|
||||
selectedItems.value = [];
|
||||
|
||||
// 修复 Bug #576:编辑模式下回显已选项目
|
||||
if (props.editData && Array.isArray(props.editData.items)) {
|
||||
const existingIds = new Set(props.editData.items.map(i => i.id));
|
||||
selectedItems.value = props.editData.items;
|
||||
unselectedItems.value = allItems.filter(item => !existingIds.has(item.id));
|
||||
// 初始化状态
|
||||
selectedItems.value = [];
|
||||
unselectedItems.value = [...allItems];
|
||||
|
||||
// 修复 Bug #576:编辑模式下准确回显已选项目
|
||||
if (editData && Array.isArray(editData.items) && editData.items.length > 0) {
|
||||
// 统一转换为 String 类型比对,避免后端返回 Number 而前端为 String 导致匹配失败
|
||||
const existingIds = new Set(editData.items.map(i => String(i.id)));
|
||||
|
||||
selectedItems.value = editData.items.map(item => ({
|
||||
...item,
|
||||
id: String(item.id)
|
||||
}));
|
||||
|
||||
unselectedItems.value = allItems.filter(item => !existingIds.has(String(item.id)));
|
||||
}
|
||||
} catch (error) {
|
||||
ElMessage.error('获取检验目录失败');
|
||||
console.error('获取检验目录失败:', error);
|
||||
ElMessage.error('加载检验项目失败');
|
||||
}
|
||||
};
|
||||
|
||||
watch(visible, (val) => {
|
||||
if (val) {
|
||||
fetchItems();
|
||||
}
|
||||
});
|
||||
|
||||
const addToSelected = (item) => {
|
||||
selectedItems.value.push(item);
|
||||
unselectedItems.value = unselectedItems.value.filter(i => i.id !== item.id);
|
||||
@@ -81,16 +91,25 @@ const removeFromSelected = (item) => {
|
||||
|
||||
const submitRequest = async () => {
|
||||
try {
|
||||
const payload = {
|
||||
items: selectedItems.value,
|
||||
...props.editData
|
||||
};
|
||||
await submitLabRequest(payload);
|
||||
ElMessage.success(props.editData ? '修改成功' : '提交成功');
|
||||
emit('submit');
|
||||
await submitLabRequest({
|
||||
...props.editData,
|
||||
items: selectedItems.value
|
||||
});
|
||||
ElMessage.success('提交成功');
|
||||
visible.value = false;
|
||||
emit('submit');
|
||||
} catch (error) {
|
||||
ElMessage.error(props.editData ? '修改失败' : '提交失败');
|
||||
ElMessage.error('提交失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 暴露 open 方法供父组件直接调用
|
||||
defineExpose({
|
||||
open: (data) => {
|
||||
visible.value = true;
|
||||
if (data) {
|
||||
fetchItems(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user