json - angular 2动态列数据表
问题描述
目前我有一个带有硬编码列标题并填写数据的数据表。我想更改此表以使其动态化,以便用户可以选择列来构建表。我想知道如何或以何种方式更改我的 json 对象以确保创建动态列数据表。
这是我尝试过的,但没有加载数据。
<table>
<thead>
<tr>
<th *ngFor="let col of columnArray">{{col}}</th>
</tr>
</thead>
<table>
<tbody>
<tr *ngFor="let col of columnArray">
<td *ngFor="let data of columnData"> {{col.data}} </td>
</tr>
</tbody>
目前,由于我的表格数据来自一个带有硬编码标题的对象,因此这是我当前的工作对象:
data = [ {'id': 'idValue', 'name': 'nameValue', 'date': 'dateValue', 'description': 'descriptionValue'}, ...
]
但由于我不知道用户将选择哪些列来创建表,所以它可能是列:id、name、description。或列:id、name。我需要使数据灵活,以便当用户选择要在表中显示的列时
解决方案
数据的工作格式:
columnArray = [ {'header': 'headerValue', 'data': 'dataValue'}, ...
]
那么模板可以是:
<table>
<thead>
<tr><th *ngFor="let col of columnArray">{{col.header}}></th></tr>
</thead>
<tbody>
<tr>
<td *ngFor="let col of columnArray"> {{col.data}} </td>
</tr>
</tbody>
</table>
如果您可以提供数据格式,则可以提供更合适的解决方案。
编辑#1:
根据您的数据格式,我将从您的数据数组中的对象中提取键作为标题。
headers = Object.keys(data[0]);
那么html应该是:
<table>
<thead>
<tr><th *ngFor="let col of headers">{{col}}></th></tr>
</thead>
<tbody>
<tr *ngFor="let obj of data">
<td *ngFor="let col of headers"> {{obj[col]}} </td>
</tr>
</tbody>
</table>
推荐阅读
- c# - 如何解决字段 'XXX' 从未分配给,并且始终具有其默认值?
- prometheus - 我正在使用 prometheus 和 wmi 导出器。我该如何解决这个错误?
- reactjs - 为什么我不能为我的多态组件属性使用默认初始化程序?
- javascript - 如何在屏幕上显示游戏服务器的 ping?
- spring - 设置 WebClient ReactorClientHttpConnector 超时并从配置中使用 JacksonProperties?
- r - 使用 ggplot 划分每组的直方图计数
- python - 链接内存脚本
- swift - 如何使collectionview单元格迅速靠近另一个单元格
- node.js - Microsoft Graph API 令牌在 3600 秒后过期 - NodeJS
- css - CSS渐变外边框