Getting Sum Of Grouped Data Angularjs - Angular-filter
i am using angular-filter to group data. while i was able to group data and get data length(orderfood), i am not able to get sum of the qty in my grouped data. my plunk demo the re
Solution 1:
You can use javascript Array.reduce method to generate the sum of quantity. here is the Plunk
<div ng-repeat="(key,data) in getOrderFoods() | groupBy:'name'">
<p>{{key}} - {{reduce(data)}}</p>
</div>
$scope.reduce= function(data){
return data.reduce(function(previousValue,currentValue, currentIndex, array){
return previousValue + parseInt(currentValue.qty);
}, 0);
}
Solution 2:
In your solution used data.length
that return array length not total qty
because of groupBy
generate an array based on groupBy conditioncan.
<divng-repeat="(key,data) in getOrderFoods() | groupBy:'name'"><p>{{key}} - {{data.length}}</p></div>
in this repeat generate two array with two items in each array because of you have two type item name and each type two times.
You can use another function to calculate total quantity that will call from in ng-repeat
. In my solution used getTotalQuantity(data)
likeIn html:
<divng-repeat="(key,data) in getOrderFoods() | groupBy:'name'"><p>{{key}} - {{getTotalQuantity(data)}}</p></div>
in controller:
$scope.getTotalQuantity = function(items) {
var total =0;
angular.forEach(items, function(item) {
total+= parseInt(item.qty);
});
return total;
}
Post a Comment for "Getting Sum Of Grouped Data Angularjs - Angular-filter"