修复下拉框选择数据设备Id未生效的问题
This commit is contained in:
parent
e12b7cedfa
commit
77e93996a3
@ -107,14 +107,26 @@ const handleValueChange = (value: string) => {
|
||||
const selectedDevice = options.value.find(option => option.value === value);
|
||||
if (selectedDevice) {
|
||||
emit('device-change', selectedDevice);
|
||||
console.log('DeviceSelect 发送设备信息:', selectedDevice);
|
||||
}
|
||||
} else {
|
||||
emit('device-change', null);
|
||||
console.log('DeviceSelect 清空设备信息');
|
||||
}
|
||||
};
|
||||
|
||||
// 初始化加载数据
|
||||
fetchData();
|
||||
|
||||
// 暴露方法给父组件调用
|
||||
defineExpose({
|
||||
getSelectedDevice: () => {
|
||||
if (props.value) {
|
||||
return options.value.find(option => option.value === props.value);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
@ -26,6 +26,7 @@ const selectedDeviceInfo = ref<any>(null);
|
||||
// 获取设备信息的完整数据(用于根据设备ID获取设备信息)
|
||||
const fetchDeviceOptions = async () => {
|
||||
try {
|
||||
console.log('开始获取设备信息...');
|
||||
const { data } = await postMetersPage({
|
||||
body: {
|
||||
pageIndex: 1,
|
||||
@ -35,7 +36,10 @@ const fetchDeviceOptions = async () => {
|
||||
|
||||
if (data?.items) {
|
||||
deviceOptions.value = data.items;
|
||||
console.log('设备信息选项:', deviceOptions.value);
|
||||
console.log('设备信息获取成功,总数:', data.items.length);
|
||||
console.log('设备信息选项前3项:', deviceOptions.value.slice(0, 3));
|
||||
} else {
|
||||
console.log('设备信息为空');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取设备信息失败:', error);
|
||||
@ -47,11 +51,7 @@ const getDeviceInfoById = (deviceId: string) => {
|
||||
return deviceOptions.value.find((device) => device.id === deviceId);
|
||||
};
|
||||
|
||||
// 处理设备选择变化
|
||||
const handleDeviceChange = (deviceInfo: any) => {
|
||||
selectedDeviceInfo.value = deviceInfo;
|
||||
console.log('设备选择变化:', deviceInfo);
|
||||
};
|
||||
|
||||
|
||||
const route = useRoute();
|
||||
const { DeviceType, DeviceId, FocusAddress, SystemName } = route.query;
|
||||
@ -115,6 +115,57 @@ const formOptions: VbenFormProps = {
|
||||
relevantFields.has(field),
|
||||
);
|
||||
|
||||
// 新增:DeviceId变化时同步selectedDeviceInfo
|
||||
if (changedFields.includes('DeviceId')) {
|
||||
const deviceId = values.DeviceId;
|
||||
console.log('DeviceId变化:', deviceId);
|
||||
|
||||
if (deviceId) {
|
||||
// 先尝试从 deviceOptions 中查找(备用方案)
|
||||
let device = deviceOptions.value.find(d => d.id === deviceId);
|
||||
|
||||
// 如果没找到,尝试从 DeviceSelect 组件中获取
|
||||
if (!device && gridApi?.formApi) {
|
||||
try {
|
||||
// 获取 DeviceSelect 组件的实例
|
||||
const deviceSelectRef = gridApi.formApi.getFieldComponentRef('DeviceId');
|
||||
console.log('DeviceSelect组件实例:', deviceSelectRef);
|
||||
if (deviceSelectRef && deviceSelectRef.getSelectedDevice) {
|
||||
device = deviceSelectRef.getSelectedDevice();
|
||||
console.log('从DeviceSelect组件获取设备信息:', device);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('无法从DeviceSelect组件获取设备信息:', error);
|
||||
}
|
||||
}
|
||||
|
||||
if (device) {
|
||||
selectedDeviceInfo.value = device;
|
||||
console.log('同步设备信息成功:', device);
|
||||
} else {
|
||||
console.log('未找到匹配的设备,deviceId:', deviceId);
|
||||
// 如果还是没找到,尝试延迟获取(组件可能还没完全更新)
|
||||
setTimeout(() => {
|
||||
try {
|
||||
const deviceSelectRef = gridApi.formApi.getFieldComponentRef('DeviceId');
|
||||
if (deviceSelectRef && deviceSelectRef.getSelectedDevice) {
|
||||
const delayedDevice = deviceSelectRef.getSelectedDevice();
|
||||
if (delayedDevice) {
|
||||
selectedDeviceInfo.value = delayedDevice;
|
||||
console.log('延迟获取设备信息成功:', delayedDevice);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('延迟获取设备信息失败:', error);
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
} else {
|
||||
selectedDeviceInfo.value = null;
|
||||
console.log('DeviceId为空,清空selectedDeviceInfo');
|
||||
}
|
||||
}
|
||||
|
||||
if (hasRelevantChange) {
|
||||
console.log('表单字段变化:', { values, changedFields });
|
||||
console.log(
|
||||
@ -130,10 +181,6 @@ const formOptions: VbenFormProps = {
|
||||
}, 0);
|
||||
}
|
||||
},
|
||||
// 添加事件处理函数
|
||||
events: {
|
||||
handleDeviceChange,
|
||||
},
|
||||
};
|
||||
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
@ -187,20 +234,31 @@ const gridOptions: VxeGridProps<any> = {
|
||||
let finalFocusAddress = formValues.FocusAddress;
|
||||
|
||||
// 优先使用选中的设备信息
|
||||
console.log('API调用时的selectedDeviceInfo:', selectedDeviceInfo.value);
|
||||
console.log('API调用时的formValues.DeviceId:', formValues.DeviceId);
|
||||
|
||||
const deviceInfo = selectedDeviceInfo.value || (formValues.DeviceId ? getDeviceInfoById(formValues.DeviceId) : null);
|
||||
console.log('最终使用的deviceInfo:', deviceInfo);
|
||||
|
||||
if (deviceInfo) {
|
||||
finalFocusAddress = deviceInfo.focusAddress;
|
||||
console.log('使用focusAddress:', finalFocusAddress);
|
||||
|
||||
if (deviceTypeNumber === 10) {
|
||||
// 集中器类型使用focusId
|
||||
if (deviceInfo.focusId) {
|
||||
finalDeviceId = deviceInfo.focusId;
|
||||
console.log('集中器类型,使用focusId:', finalDeviceId);
|
||||
}
|
||||
} else {
|
||||
// 其他设备类型使用meterId
|
||||
if (deviceInfo.meterId) {
|
||||
finalDeviceId = deviceInfo.meterId;
|
||||
console.log('其他类型,使用meterId:', finalDeviceId);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log('没有找到设备信息,使用原始DeviceId:', finalDeviceId);
|
||||
}
|
||||
try {
|
||||
const { data } = await postTreeModelDeviceDataInfoPage({
|
||||
|
||||
@ -112,7 +112,6 @@ export const querySchema = computed(() => [
|
||||
componentProps: {
|
||||
placeholder: $t('common.pleaseSelect') + $t('abp.log.deviceInfo'),
|
||||
allowClear: true,
|
||||
onDeviceChange: 'handleDeviceChange',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user