【发布时间】:2020-05-03 00:57:38
【问题描述】:
我需要根据来自 API 的数据在 html 中创建一个表(使用较少的后端 ts 代码)。 数据将以以下方式出现(针对这个问题进行了模拟)
{ strTableName: "TestTable", strComment:"Approved",
lstTableHeaders:[
{strFieldName : "Sl No.", strType:"Numeric", lstComments:[]},
{strFieldName : "Description", strType:"string", lstComments:[]},
{strFieldName : "Units", strType:"Numeric", lstComments:[]},
{strFieldName : "Total", strType:"Amount", lstComments:[]},
], TableData:
{
lstRowData:[
{iID: 1, lstCellData:[
{strFieldName:"Sl No.", strValue:"1", strComment:"", strBackgroundColor:"green"},
{strFieldName:"Description", strValue:"Item 1", strComment:"", strBackgroundColor:"red"},
{strFieldName:"Units", strValue:"1", strComment:"", strBackgroundColor:"green"},
{strFieldName:"Total", strValue:"500", strComment:"", strBackgroundColor:"yellow"},
]},
{iID: 2, lstCellData:[
{strFieldName:"Sl No.", strValue:"2", strComment:"", strBackgroundColor:"green"},
{strFieldName:"Description", strValue:"Item 2", strComment:"", strBackgroundColor:"red"},
{strFieldName:"Units", strValue:"3", strComment:"", strBackgroundColor:"green"},
{strFieldName:"Total", strValue:"1500", strComment:"", strBackgroundColor:"yellow"},
]},
]
}
}
当鼠标悬停在单元格上时,背景颜色将用作单元格的背景颜色,注释将用于弹出数据。这属于样式,但我需要根据这些数据创建表。
这可能吗?
(尝试从 github、stackoverflow 搜索很多问题,甚至从 angular 浏览文档。最后才提出这个问题。)
编辑:在 html 中添加以下代码以显示标题。仍在努力获取表中的数据。
<mat-table #table>
<ng-container *ngFor="let column of item.lstTableHeaders" [matColumnDef]="column.strFieldName">
<mat-header-cell *matHeaderCellDef>{{ column.strFieldName }}</mat-header-cell>
<mat-cell *matCellDef="let column">{{ column.strFieldName }}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="item.displayedTableHeaders()"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedTableHeaders();"></mat-row>
</mat-table>
在 ts 文件中:我为表格模型添加了一个函数:
public displayedTableHeaders() : string[]{
return this.lstTableHeaders.map(x=> x.strFieldName);
}
任何帮助将不胜感激。
【问题讨论】:
标签: html angular dynamic angular8