angular - 使用角度材料表显示数据
问题描述
我有一个不同的数据结构,我需要通过mat-table
.
dataSource= [[1,2],[3,4],[5,6]]
这里,dataSource[0]
永远是标题。其余的数组项是它的行。
所以上述数据的输出应该如下所示
1 2 Heading of the table
3 4
5 6
上述数据结构可以使用mat-table
吗?请帮忙
注意:- 我查看了 mat-table 文档,但我能找到任何对我有帮助的东西
解决方案
您可以实现这一点,您需要提取列名的值并将其与数据分开。
export class TableBasicExample {
data = [[1,2],[3,4],[5,6]];
dataSource = this.data.splice(1);
displayedColumns: string[] = this.data[0].map(value => String(value));
}
在模板中动态绑定这些数据源和列名。
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- Position Column -->
<ng-container matColumnDef="{{displayedColumns[0]}}">
<th mat-header-cell *matHeaderCellDef> {{displayedColumns[0]}} </th>
<td mat-cell *matCellDef="let element"> {{element[0]}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="{{displayedColumns[1]}}">
<th mat-header-cell *matHeaderCellDef> {{displayedColumns[1]}} </th>
<td mat-cell *matCellDef="let element"> {{element[1]}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
请在此处找到有效的 stackblitz:https ://stackblitz.com/edit/angular-nmnymd
推荐阅读
- php - 如何使 PHP POST 请求在 CRUD API 中作为连接 MySQL 数据库的层正常工作?
- r - 计算行之间的差异,但按组保留原始值
- math - 如何在更高维度的超球面上均匀分布点?
- python - 设置 LSTM 层的初始状态
- r - 使用 mutate 以动态命名列为条件创建新类别
- c++ - 为什么在写入文件时删除空格无法读取文件?
- android - 即使我禁用了持久性,Firestore 仍在从缓存中检索文档
- assembly - 试图通过 cr0 寄存器禁用分页
- r - 错误:在“模拟”中:缺少参数“参数”,没有默认值
- android - 切换到选项卡式布局中的另一个选项卡时未调用片段 onStart() 和 onStop()