我想使用angularJS并使用ng-repeat来显示我的数据。我有一个JSON,我必须按ID /名称对特定行项目进行分组。 我不知道如何实现特定的输出。我添加了一个剪切的输出我需要。
var obj = [{
"id": 101,
"name": "Victor",
"address": "Mumbai"
"order": [{
"itemName": "Bulb",
"quantity": 10
},
{
"itemName": "Soap",
"quantity": 3
},
{
"itemName": "Towel",
"quantity": 1
}
]
},
{
"id": 102,
"name": "Francis",
"address": "Nagpur"
"order": [{
"itemName": "Remote",
"quantity": 1
},
{
"itemName": "Bread",
"quantity": 2
}
]
}
]
<table border="1" width="100%">
<tr>
<th>Id</th>
<th>Name</th>
<th>Address</th>
<th>Item Name</th>
<th>Quantity</th>
</tr>
<!--- below is the output that i want -- uncomment and see the output -->
<tr>
<td>101</td><td>Victor</td><td>Mumbai</td><td>Bulb</td><td>10</td>
</tr>
<tr>
<td></td><td></td><td></td><td>Soap</td><td>3</td>
</tr>
<tr>
<td></td><td></td><td></td><td>Towel</td><td>3</td>
</tr>
<tr>
<td>102</td><td>Francis</td><td>Nagpur</td><td>Remote</td><td>1</td>
</tr>
<tr>
<td></td><td></td><td></td><td>Bread</td><td>2</td>
</tr>
</table>
答案 0 :(得分:5)
说明:
您必须使用ng-repeat来循环数据。 但是你还必须使用ng-repeat-start和ng-repeat-end,因为你的前3列不会在每一行中重复。
angular.module("tableApp", []).controller("tableController", ['$scope', function($scope) {
var temp = [{
"id": 101,
"name": "Victor",
"address": "Mumbai",
"order": [{
"itemName": "Bulb",
"quantity": 10
},
{
"itemName": "Soap",
"quantity": 3
},
{
"itemName": "Towel",
"quantity": 1
}
]
},
{
"id": 102,
"name": "Francis",
"address": "Nagpur",
"order": [{
"itemName": "Remote",
"quantity": 1
},
{
"itemName": "Bread",
"quantity": 2
}
]
}
]
/*
var arr = [];
for(var i=0;i<temp.length;i++){
var obj = {}
obj.id = temp[i].id;
obj.address = temp[i].address;
obj.name = temp[i].name;
for(var j=0;j<temp[i].order.length;j++){
obj.itemName = temp[i].order[j].itemName;
obj.quantity = temp[i].order[j].quantity;
}
arr.push(obj);
}*/
$scope.arr = temp;
}])
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="tableApp">
<table ng-controller="tableController" border="1" width="100%">
<tr>
<th>Id</th>
<th>Name</th>
<th>Address</th>
<th>Item Name</th>
<th>Quantity</th>
</tr>
<!--- below is the output that i want -- uncomment and see the output -->
<tr ng-if="0" ng-repeat-start="data in arr">
<tr ng-repeat="a in data.order">
<td ng-if="!$first"></td>
<td ng-if="$first">{{data.id}}</td>
<td ng-if="!$first"></td>
<td ng-if="$first">{{data.name}}</td>
<td ng-if="!$first"></td>
<td ng-if="$first">{{data.address}}</td>
<td>{{a.itemName}}</td>
<td>{{a.quantity}}</td>
</tr>
<tr ng-if="0" ng-repeat-end></tr>
</table>
</div>
&#13;
答案 1 :(得分:2)
在填充数据之前,如果您没有进一步操作,我更愿意根据需要拆分数据以进行显示。
以下js小提琴将根据您的要求为您提供输出。 https://jsfiddle.net/waLvc49x/
<tbody ng-repeat="rows in values">
<tr ng-repeat="row in rows.order">
<td>{{!$index?rows.id:''}}</td>
<td>{{!$index?rows.name:''}}</td>
<td>{{!$index?rows.address:''}}</td>
<td>{{row.itemName}}</td>
<td>{{row.quantity}}</td>
</tr>
</tbody>