Angular表格组件表头与表行数据关联异常的解决方法咨询
Angular表格表头与数据关联异常问题
我基于Angular开发了一个表格组件,通过paymentTableMetadata(表格schema)和paymentTableData(按行/单元格存储的业务数据)生成HTML表格,但目前表头与表行数据对应错误,具体问题如下:
- Currency的value1错误显示在Amount列下
- Result列本该为空,但显示异常
- Process Key列未正确显示LowPaymentBpm值
component.ts 代码
import { Component, OnInit } from '@angular/core'; import { CdkDragDrop } from '@angular/cdk/drag-drop'; import { PaymentTableService } from 'src/app/services/payment-table-service/payment-table-service'; @Component({ selector: 'payment-table', templateUrl: './payment-table.component.html', styleUrls: ['./payment-table.component.scss'] }) export class PaymentTableComponent implements OnInit { paymentTableMetadata = [ { "cntrlFldCode": "Curr", "cntrlFldLabel": "Currency", "fieldDataType": "StringType", "cntrlFldTypeEnum": "InputField" }, { "cntrlFldCode": "Amount", "cntrlFldLabel": "Amount", "fieldDataType": "DecimalType", "cntrlFldTypeEnum": "InputField" }, { "cntrlFldCode": "Output", "cntrlFldLabel": "Result", "fieldDataType": "StringType", "cntrlFldTypeEnum": "OutputField" } ]; paymentTableData = { "dmnCode": "001", "dmnDsc": "Payment Amount Control", "dmnRows": [ { "rowCode": "1", "rowDsc": "USD Amount <10,00", "rowOrder": 1, "processKey": "LowPaymentBpm", "outputValue": null, "cells": [ { "cellCode": "amt", "cntrlFldCode": "Amount", "cntrlFldLabel": "Amount", "fieldDataType": "DecimalType", "cntrlFldTypeEnum": "InputField", "operatorEnum": "LessThanEqual", "dateTypeEnum": null, "value1TypeEnum": null, "value1": "10000", "value1Set": null, "value1Offset": null, "value2TypeEnum": null, "value2": null, "value2Set": null, "value2Offset": null, "errors": null }, { "cellCode": "curr", "cntrlFldCode": "Curr", "cntrlFldLabel": "Currency", "fieldDataType": "StringType", "cntrlFldTypeEnum": "InputField", "operatorEnum": "Equals", "dateTypeEnum": null, "value1TypeEnum": null, "value1": "USD", "value1Set": null, "value1Offset": null, "value2TypeEnum": null, "value2": null, "value2Set": null, "value2Offset": null, "errors": null } ] } ], "errors": null }; constructor(private paymentTableService : PaymentTableService) { } onDrop(event: CdkDragDrop<string[]>) { this.paymentTableService.paymentTableMetadata; } ngOnInit() { } }
component.html 代码
<table class="table table-striped table-no-bordered table-hover my-table"> <thead> <tr *ngIf="paymentTableMetadata"> <th *ngFor="let paymentCell of paymentTableMetadata"> <span class="table-header">{{paymentCell.cntrlFldLabel}}</span> </th> <th> <span class="table-header">Process Key</span> </th> </tr> </thead> <tbody cdkDropList (cdkDropListDropped)="onDrop($event)" id="dataTable"> <ng-container *ngIf="paymentTableData.dmnRows.length >0 ; else empty"> <tr cdkDrag cdkDragLockAxis="y" *ngFor="let tableRow of paymentTableData.dmnRows" id="{{ tableRow.rowCode }}"> <td *ngFor="let cell of tableRow.cells">{{cell.value1}}</td> <td>{{tableRow.processKey}}</td> </tr> </ng-container> <ng-template #empty> <p class="no-data"> No Data In Table</p> </ng-template> </tbody> </table>
问题根源与解决思路
1. 列顺序不匹配导致数据错位
当前HTML直接遍历行内cells输出数据,但cells的顺序(Amount在前,Currency在后)与paymentTableMetadata的表头顺序(Currency在前,Amount在后)不一致,导致数据和表头错位。
解决方法:
放弃直接遍历cells,改为遍历paymentTableMetadata,通过cntrlFldCode匹配对应单元格数据:
- 在component.ts中添加匹配方法:
getCellValue(cells: any[], fieldCode: string): string | null { const targetCell = cells.find(cell => cell.cntrlFldCode === fieldCode); return targetCell ? targetCell.value1 : null; } - 修改HTML中的单元格遍历逻辑:
<td *ngFor="let metadata of paymentTableMetadata"> {{ getCellValue(tableRow.cells, metadata.cntrlFldCode) ?? '' }} </td>
2. Result列空值处理异常
paymentTableMetadata包含Result列(cntrlFldCode为Output),但行数据的cells中无对应单元格,需显式处理空值避免异常显示。上述?? ''操作符会将空值转为空字符串,解决该问题。
3. Process Key列显示排查
当前代码中Process Key列的TD数量与表头TH数量匹配,若仍不显示,可排查:
- 确认
tableRow.processKey存在有效值(当前数据中为LowPaymentBpm) - 检查CSS是否隐藏该列或覆盖内容
- 确认DOM结构无嵌套错误
额外优化建议
给数据结构定义TypeScript接口,提升代码可读性与类型校验:
interface TableMetadata { cntrlFldCode: string; cntrlFldLabel: string; fieldDataType: string; cntrlFldTypeEnum: string; } interface TableCell { cellCode: string; cntrlFldCode: string; value1?: string; // 按需添加其他字段 } interface TableRow { rowCode: string; processKey: string; cells: TableCell[]; } // 变量类型声明 paymentTableMetadata: TableMetadata[] = [...]; paymentTableData: { dmnRows: TableRow[] } = {...};
内容的提问来源于stack exchange,提问作者Wissam
相关产品推荐
相关产品推荐

