-
-
Save Cacodaimon/7309268 to your computer and use it in GitHub Desktop.
<!-- $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> |
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; | |
}; | |
}); |
Thank you SO much! You solved a sum problem with a statistics data table I had. Great!
hello, thanks for this helpful filter.
But it seems like, it doesn't sum decimals for a floating value.
35.46 + 20.35 brings 55 instead of 55.81
I've improved it with support for objects instead of just arrays, and support for float values
https://gist.github.com/WindSaber/ae4c5d7ea689b36990b5545d1e4a91ac
Thanks!
I have a issue when key propierty don't exists and other times yes... so... I just add some like this:
if (data[i][key]) {
sum += parseInt(data[i][key]);
}
is there any textbox sum example with angularjs ng-repeat
Amazing man! thank you!
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
Thanks for sharing. Was very useful.