Please see what I need to do at the end of this question.
I have the following relationship between Category and Products (1-n).
This is the html table with the inventory
<table>
<thead>
<tr>
<th>#</th>
<th>Category</th>
<th>Products quantity</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="category in categories" data-ng-repeat="getProductsByCategory(category)">
<td>{{category.name}}</td>
<td>{{products[category.id] | sumByKey:'quantity'}}</td>
</tr>
</tbody>
<tfooter>
<tr>
<td colspan="2">total</td>
<td></td>
</tr>
</footer>
And this is the result, as you can see I'm using the 'sumByKey' filter in order to get the sum of all products by its category
The respective function to get all products by its category
$scope.products = [];
$scope.getProductsByCategory = function(category){
$http.get("getProductsByCategory/"+category.id)
.success(function (data, status, headers, config) {
$scope.products[category.id] = data;
})
.error(function (data, status, header, config) {
$log.error("Error");
});
};
And the filter to sum all quantities
app.filter("sumByKey", function() {
return function(data, key) {
if (typeof(data) === 'undefined' || typeof(key) === 'undefined') {
return 0;
}
var sum = 0;
for (var i = data.length - 1; i >= 0; i--) {
sum += parseInt(data[i][key]);
}
return sum;
};
});
In order to get the total in the inventory table I have been trying to use the same filter (sumByKey) but it doesn't work. Any idea to get the respective result?

