Angular ng-repeat table:来自 object 的动态列
Angular ng-repeat table: dynamic columns from object
我正在尝试创建一个看起来像这样的 table:
20 23 25 26
734.53 279.20 936.95 584.50
基于此Object:
{
"20": 734.5339864003384,
"23": 279.20378246766563,
"25": 936.9526667106079,
"26": 584.5060233468447,
"27": 279.20378246766563,
"28": 2055.970661511549,
"29": 1405.0981690224412,
"30": 549.4917661928141,
"31": 2329.1464674575695,
"32": 147.8822594632703,
"33": 698.0104349592335
}
显然列的标题需要根据JSON的键和值进行动态对应。
我有以下代码,但我完全不确定我是否在正确的轨道上:
JS
$scope.modal = {};
$scope.modal.keys = [];
$scope.modal.data = [];
for (var key in assets.total) {
if (assets.total.hasOwnProperty(key)) {
$scope.modal.keys.push(key);
$scope.modal.data.push(assets.total[key]);
}
}
HTML
<table class="table table-striped">
<thead>
<tr>
<th ng-repeat="th in modal.key">{{th}}</th>
</tr>
</thead>
<tbody></tbody>
<tr ng-repeat="x in modal.data">
<td ng-repeat="th in modal.key">{{x[th]}}</td>
</tr>
</tbody>
</table>
它只是不起作用我只用一列得到了一个奇怪的结果。
您可以试试这个解决方案
使用 ng-repeat="(key, value) in Array"
获取对象中的键值。
我已经在 Working Demo on Stackblitz
上创建了一个演示
并删除 for (var key in assets.total)
循环。
html代码
<table class="table table-striped">
<thead>
<tr>
<th ng-repeat="(key, value) in data"> {{key}} </th>
</tr>
<tr>
<td ng-repeat="(key, value) in data"> {{ value| number: 2 }} </td>
</tr>
</tbody>
</table>
Js file code
$scope.data = {
"20": 734.5339864003384,
"23": 279.20378246766563,
"25": 936.9526667106079,
"26": 584.5060233468447,
"27": 279.20378246766563,
"28": 2055.970661511549,
"29": 1405.0981690224412,
"30": 549.4917661928141,
"31": 2329.1464674575695,
"32": 147.8822594632703,
"33": 698.0104349592335
}
我正在尝试创建一个看起来像这样的 table:
20 23 25 26
734.53 279.20 936.95 584.50
基于此Object:
{
"20": 734.5339864003384,
"23": 279.20378246766563,
"25": 936.9526667106079,
"26": 584.5060233468447,
"27": 279.20378246766563,
"28": 2055.970661511549,
"29": 1405.0981690224412,
"30": 549.4917661928141,
"31": 2329.1464674575695,
"32": 147.8822594632703,
"33": 698.0104349592335
}
显然列的标题需要根据JSON的键和值进行动态对应。
我有以下代码,但我完全不确定我是否在正确的轨道上:
JS
$scope.modal = {};
$scope.modal.keys = [];
$scope.modal.data = [];
for (var key in assets.total) {
if (assets.total.hasOwnProperty(key)) {
$scope.modal.keys.push(key);
$scope.modal.data.push(assets.total[key]);
}
}
HTML
<table class="table table-striped">
<thead>
<tr>
<th ng-repeat="th in modal.key">{{th}}</th>
</tr>
</thead>
<tbody></tbody>
<tr ng-repeat="x in modal.data">
<td ng-repeat="th in modal.key">{{x[th]}}</td>
</tr>
</tbody>
</table>
它只是不起作用我只用一列得到了一个奇怪的结果。
您可以试试这个解决方案
使用 ng-repeat="(key, value) in Array"
获取对象中的键值。
我已经在 Working Demo on Stackblitz
上创建了一个演示并删除 for (var key in assets.total)
循环。
html代码
<table class="table table-striped">
<thead>
<tr>
<th ng-repeat="(key, value) in data"> {{key}} </th>
</tr>
<tr>
<td ng-repeat="(key, value) in data"> {{ value| number: 2 }} </td>
</tr>
</tbody>
</table>
Js file code
$scope.data = {
"20": 734.5339864003384,
"23": 279.20378246766563,
"25": 936.9526667106079,
"26": 584.5060233468447,
"27": 279.20378246766563,
"28": 2055.970661511549,
"29": 1405.0981690224412,
"30": 549.4917661928141,
"31": 2329.1464674575695,
"32": 147.8822594632703,
"33": 698.0104349592335
}