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