修复错误
This commit is contained in:
parent
ba7ebdb348
commit
150c5a96f5
@ -52,20 +52,34 @@ export const generateDynamicColumns = (data: DynamicDeviceData[]): ColumnConfig[
|
|||||||
return fields
|
return fields
|
||||||
.filter(field => !excludeFields.includes(field))
|
.filter(field => !excludeFields.includes(field))
|
||||||
.map(field => {
|
.map(field => {
|
||||||
const columnConfig: any = {
|
// 确保字段名是有效的字符串
|
||||||
field,
|
const safeField = String(field || '').trim();
|
||||||
title: fieldNameMapping[field] || field,
|
if (!safeField) return null;
|
||||||
|
|
||||||
|
const columnConfig: ColumnConfig = {
|
||||||
|
field: safeField,
|
||||||
|
title: fieldNameMapping[safeField] || safeField,
|
||||||
minWidth: 150,
|
minWidth: 150,
|
||||||
showOverflow: true,
|
showOverflow: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
// 应用字段类型配置
|
// 应用字段类型配置,确保配置是安全的
|
||||||
if (fieldTypeConfig[field]) {
|
if (fieldTypeConfig[safeField]) {
|
||||||
Object.assign(columnConfig, fieldTypeConfig[field]);
|
const typeConfig = fieldTypeConfig[safeField];
|
||||||
|
if (typeConfig.formatter && typeof typeConfig.formatter === 'function') {
|
||||||
|
columnConfig.formatter = typeConfig.formatter;
|
||||||
|
}
|
||||||
|
if (typeConfig.width !== undefined) {
|
||||||
|
columnConfig.width = typeConfig.width;
|
||||||
|
}
|
||||||
|
if (typeConfig.minWidth !== undefined) {
|
||||||
|
columnConfig.minWidth = typeConfig.minWidth;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return columnConfig;
|
return columnConfig;
|
||||||
});
|
})
|
||||||
|
.filter(Boolean) as ColumnConfig[]; // 过滤掉null值
|
||||||
};
|
};
|
||||||
|
|
||||||
// 获取所有可能的字段(用于预定义列)
|
// 获取所有可能的字段(用于预定义列)
|
||||||
|
|||||||
@ -59,26 +59,39 @@ const dynamicColumns = ref<any[]>([]);
|
|||||||
|
|
||||||
// 固定列定义(始终显示)- 基于 IoTDBTreeModelDeviceDataDto 类型
|
// 固定列定义(始终显示)- 基于 IoTDBTreeModelDeviceDataDto 类型
|
||||||
const fixedColumns = [
|
const fixedColumns = [
|
||||||
{ title: '序号', type: 'seq', width: 50 },
|
{ title: '序号', type: 'seq', width: 50, field: 'seq' },
|
||||||
{ field: 'Timestamps', title: $t('abp.IoTDBBase.Timestamps'), minWidth: 150 },
|
{ field: 'Timestamps', title: $t('abp.IoTDBBase.Timestamps'), minWidth: 150, showOverflow: true },
|
||||||
{ field: 'SystemName', title: $t('abp.IoTDBBase.SystemName'), minWidth: 150 },
|
{ field: 'SystemName', title: $t('abp.IoTDBBase.SystemName'), minWidth: 150, showOverflow: true },
|
||||||
{ field: 'ProjectId', title: $t('abp.IoTDBBase.ProjectId'), minWidth: 150 },
|
{ field: 'ProjectId', title: $t('abp.IoTDBBase.ProjectId'), minWidth: 150, showOverflow: true },
|
||||||
{ field: 'DeviceType', title: $t('abp.IoTDBBase.DeviceType'), minWidth: 150 },
|
{ field: 'DeviceType', title: $t('abp.IoTDBBase.DeviceType'), minWidth: 150, showOverflow: true },
|
||||||
{
|
{
|
||||||
field: 'IoTDataType',
|
field: 'IoTDataType',
|
||||||
title: $t('abp.IoTDBBase.IoTDataType'),
|
title: $t('abp.IoTDBBase.IoTDataType'),
|
||||||
minWidth: 150,
|
minWidth: 150,
|
||||||
|
showOverflow: true,
|
||||||
},
|
},
|
||||||
{ field: 'DeviceId', title: $t('abp.IoTDBBase.DeviceId'), minWidth: 150 },
|
{ field: 'DeviceId', title: $t('abp.IoTDBBase.DeviceId'), minWidth: 150, showOverflow: true },
|
||||||
];
|
];
|
||||||
|
|
||||||
// 合并固定列和动态列 - 使用计算属性确保响应式
|
// 合并固定列和动态列 - 使用计算属性确保响应式
|
||||||
const allColumns = computed(() => [...fixedColumns, ...dynamicColumns.value]);
|
const allColumns = computed(() => {
|
||||||
|
const columns = [...fixedColumns];
|
||||||
|
|
||||||
|
// 安全地添加动态列
|
||||||
|
if (dynamicColumns.value && Array.isArray(dynamicColumns.value)) {
|
||||||
|
const validDynamicColumns = dynamicColumns.value.filter(col =>
|
||||||
|
col && typeof col === 'object' && col.field && col.title
|
||||||
|
);
|
||||||
|
columns.push(...validDynamicColumns);
|
||||||
|
}
|
||||||
|
|
||||||
|
return columns;
|
||||||
|
});
|
||||||
|
|
||||||
// 初始化默认列(防止表格空白)
|
// 初始化默认列(防止表格空白)
|
||||||
const initDefaultColumns = () => {
|
const initDefaultColumns = () => {
|
||||||
if (dynamicColumns.value.length === 0) {
|
// 确保dynamicColumns是一个有效的数组
|
||||||
// 不再需要在这里设置默认列,因为固定列已经包含了基本字段
|
if (!Array.isArray(dynamicColumns.value)) {
|
||||||
dynamicColumns.value = [];
|
dynamicColumns.value = [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -249,6 +262,7 @@ const gridOptions: VxeGridProps<any> = {
|
|||||||
|
|
||||||
// 简化处理逻辑,直接使用接口返回的数据
|
// 简化处理逻辑,直接使用接口返回的数据
|
||||||
if (data?.items && data.items.length > 0) {
|
if (data?.items && data.items.length > 0) {
|
||||||
|
try {
|
||||||
// 动态生成列定义
|
// 动态生成列定义
|
||||||
const generatedColumns = generateDynamicColumns(data.items);
|
const generatedColumns = generateDynamicColumns(data.items);
|
||||||
|
|
||||||
@ -257,12 +271,19 @@ const gridOptions: VxeGridProps<any> = {
|
|||||||
|
|
||||||
// 使用setState更新整个gridOptions,确保列定义能够正确更新
|
// 使用setState更新整个gridOptions,确保列定义能够正确更新
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
if (gridApi && gridApi.setState) {
|
||||||
gridApi.setState({
|
gridApi.setState({
|
||||||
gridOptions: {
|
gridOptions: {
|
||||||
...gridOptions,
|
...gridOptions,
|
||||||
columns: allColumns.value,
|
columns: allColumns.value,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('更新列定义时出错:', error);
|
||||||
|
// 如果列更新失败,使用空数组
|
||||||
|
dynamicColumns.value = [];
|
||||||
|
}
|
||||||
|
|
||||||
// 直接使用接口返回的totalCount
|
// 直接使用接口返回的totalCount
|
||||||
const result = {
|
const result = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user