Created
November 4, 2013 21:12
-
-
Save Cacodaimon/7309268 to your computer and use it in GitHub Desktop.
A simple AngularJS filter for summarizing the values of an array containing objects by a key.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- $scope.myList = [{name: 'Foo', total: 1}, {name: 'Bar', total: 2}, {name: 'Baz', total: 3}] --> | |
<span class="badge badge-success pull-right">{{myList|sumByKey:'total'}}</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('caco.feed.filter', []) | |
.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; | |
}; | |
}); |
data ?
Amazing. Super.
Great Thank you,I have a problem,I have pagination with table.The sum is showing for each page,How can i get grand total instead of page wise torals
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing man! thank you!