我想根据来自 webapi 的 json 对象生成带有动态表头和行/列的动态表。
这是每次都不同的 json 对象的示例。
[
{"Country":"Australia","Toner Quantity":8},
{"Country":"China","Toner Quantity":6},
{"Country":"India","Toner Quantity":11},
{"Country":"South Korea","Toner Quantity":1}
]
有时它会像
[
{"CustomerName":"FORD","Australia":0,"China":2,"India":0,"South Korea":0},
{"CustomerName":"ICICI PRUDENTIAL","Australia":0,"China":0,"India":5,"South Korea":0},
{"CustomerName":"Kimberly Clark","Australia":0,"China":0,"India":0,"South Korea":1},
{"CustomerName":"McDonalds","Australia":1,"China":0,"India":0,"South Korea":0},
{"CustomerName":"Novartis","Australia":1,"China":0,"India":0,"South Korea":0},
{"CustomerName":"Origin Energy","Australia":3,"China":0,"India":0,"South Korea":0}
]
所以我尝试过但无法实现带有标题和行/列的动态表
我的 html 代码像
<table class="table striped">
<thead>
<tr role="row">
<th ng-repeat="th in dataconfigureListData.previewData">{{th}}</th>
</tr>
</thead>
<tbody>
<tr role="row" data-ng-repeat="previewData in dataconfigureListData.previewData">
<td> {{previewData.Country}}</td>
<td> {{previewData['Total Toner Qty']}}</td>
</tr>
</tbody>
</table>
原文由 Dixit 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以通过在另一个内部使用
ng-repeat
来做到这一点。而且,使用第一行来呈现标题。以下代码段实现了此解决方案。
下面的示例通过单击列标题动态地对列进行排序,也可以通过再次单击所选列来实现反向排序