const application = angular.module('application', []);
function Controller($scope) {
$scope.dataCollection = [{
"id": "1",
"name": "BUILDING A",
"city": "PARIS",
"date": "2015-11-09",
"stat1": 3,
"stat2": 115,
"stat3": 4,
"stat4": 111
}, {
"id": "2",
"name": "BUILDING B",
"city": "LONDON",
"date": "2013-11-09",
"stat1": 1,
"stat2": 51,
"stat3": 1,
"stat4": 50
}, {
"id": "3",
"name": "BUILDING C",
"city": "TOKYO",
"date": "2012-10-21",
"stat1": 0,
"stat2": 142,
"stat3": 0,
"stat4": 142
}, {
"id": "4",
"name": "BUILDING D",
"city": "PARIS",
"date": "2011-02-03",
"stat1": 0,
"stat2": 132,
"stat3": 0,
"stat4": 132
}, {
"id": "5",
"name": "BUILDING E",
"city": "CHICAGO",
"stat1": 0,
"stat2": 94,
"stat3": 0,
"stat4": 94
}, {
"id": "6",
"name": "BUILDING F",
"city": "LONDON",
"date": "0000-00-00",
"stat1": 0,
"stat2": 86,
"stat3": 0,
"stat4": 86
}];
var groupedData = {};
var totalData = {
"totalStat1": 0,
"totalStat2": 0,
"totalStat3": 0,
"totalStat4": 0,
};
$scope.dataCollection.forEach(function(item) {
groupedData[item.city] = groupedData[item.city] || {};
groupedData[item.city].array = groupedData[item.city].array || [];
groupedData[item.city].total = groupedData[item.city].total || {
"totalStat1": 0,
"totalStat2": 0,
"totalStat3": 0,
"totalStat4": 0,
};
groupedData[item.city].array.push(item);
groupedData[item.city].total.totalStat1 += item.stat1;
groupedData[item.city].total.totalStat2 += item.stat2;
groupedData[item.city].total.totalStat3 += item.stat3;
groupedData[item.city].total.totalStat4 += item.stat4;
totalData.totalStat1 += item.stat1;
totalData.totalStat2 += item.stat2;
totalData.totalStat3 += item.stat3;
totalData.totalStat4 += item.stat4;
});
$scope.groupedData = groupedData;
$scope.totalData = totalData;
}
table {
margin: 1rem;
border-collapse: collapse;
width: 55%;
}
table,
th,
td {
border: 1px solid black;
}
.total{
font-weight:800;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="application" ng-controller="Controller">
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>City</th>
<th>Date</th>
<th>Stat1</th>
<th>Stat2</th>
<th>Stat3</th>
<th>Stat4</th>
</tr>
</thead>
<tbody ng-repeat="(key, value) in groupedData">
<tr ng-repeat="val in value.array">
<td>{{val.id}}</td>
<td>{{val.city}}</td>
<td>{{val.name}}</td>
<td>{{val.date}}</td>
<td>{{val.stat1}}</td>
<td>{{val.stat2}}</td>
<td>{{val.stat3}}</td>
<td>{{val.stat4}}</td>
</tr>
<tr class="total">
<td>TOTAL</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>{{value.total.totalStat1}}</td>
<td>{{value.total.totalStat2}}</td>
<td>{{value.total.totalStat3}}</td>
<td>{{value.total.totalStat4}}</td>
</tr>
</tbody>
</table>
<div>
Total stat 1 : {{totalData.totalStat1}} -
Total stat 2 : {{totalData.totalStat2}} -
Total stat 3 : {{totalData.totalStat3}} -
Total stat 4 : {{totalData.totalStat4}} -
</div>
</div>