2

I add dynamically rows in my table with ng-repeat, coming from an array.

Now I want to get the sum of all sums per row (group.sum * group.perc / 100.0). I need it in a variable because I need this value for further calculations. Thank you

HTML

<tr ng-repeat="group in groupsArr">                                         
  <td class="total-rows" ng-model="taxes">{{group.sum * group.perc / 100.0 | currency :""}}</td>
</tr>

SCRIPT

var taxTotals = 0;
var taxTotals = 
  for (i=0; i<group.length; i++) {
    taxTotal = taxTotal + group[i].taxes;    
};
console.log(taxTotals);
};  

4 Answers 4

8

Create a Filter:

 app.filter('sumFilter', function() {
     return function(groups) {
         var taxTotals = 0;
         for (i=0; i<groups.length; i++) {
             taxTotal = taxTotal + groups[i].taxes;    
          };
         return taxTotals;
     };
 });

Use the $filter service:

 app.controller('myController', function($scope, $filter) {
      $scope.groups = [...];

      var taxTotals = $filter('sumFilter')($scope.groups);
      console.log(taxTotals);
 });

Use it in your HTML:

<tr ng-repeat="group in groupsArr">                                         
    <td class="total-rows" ng-model="taxes">{{group.sum * group.perc / 100.0 | currency :""}}    </td>
</tr>
 <tr>
      <b> Tax Totals: </b> {{ groupsArr | sumFilter | currency }}
 </tr>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the hint doing it with a filter. Now I get an exception Cannot read property 'length' of undefined within the filter. `angular.module('App.filters', []).filter('sumFilter', [function () {return function(groups) {var taxTotals = 0; for (i=0; i<groups.length; i++) { taxTotal = taxTotal + groups[i].taxes; }; return taxTotals; }; }]);
How are you using the filter? Are you sure you are passing in an array that is defined?
0

An addition for best answer... I am using filter in my very huge table, so it is how to implement with dynamic filters.

THE FILTER

app.filter('sumStatusFilter', function(){
return function (items, filtersStatus, filterLocations){
  var filtered = [];
  var filtered1 = [];
  var total = 0;
  if (typeof filtersStatus != 'undefined') {
    angular.forEach(items, function(item) {
      for(i = 0; i < filtersStatus.length; i ++){
        if(filtersStatus[i] == item.status_message)
          filtered.push(item);  
      }
    });
  }

  if (typeof filterLocations != 'undefined') {
    angular.forEach(filtered, function(item) {
      for(i = 0; i < filterLocations.length; i ++){
        if(filterLocations[i] == item.office_location)
          filtered1.push(item);  
      }
    });
    filtered = [];
    filtered = filtered1;
  }

  if (filtered.length == 0) {
    filtered = this.jobs
  }
  angular.forEach(filtered, function(value, key){
    total += value.restoration_reserve
  });
  return total;
}

});

in HTML

<tr><td>Total: {{ report_controller.items | sumStatusFilter:report_controller.status_message_selections:report_controller.office_selections | currency }}</td></tr>

Comments

0

UPDATE AFTER ANSWER coming from pixelbits

Thanks to pixelbits. Here is my filter, which works perfect within the view.

HTML

<tr ng-repeat="group in groupsArr">                                         
    <td class="total-rows" ng-model="taxes">{{group.sum * group.perc / 100.0 | currency :""}}    </td>
</tr>
 <tr>
      <b> Tax Totals: </b> {{ groupsArr | sumFilter | currency }}
 </tr>

Filter

angular.module('App.filters', []).filter('sumFilter', [function () {
// filter for tax sum
     return function(groups, lenght) {
         var taxTotal = 0; 
         for (i=0; i < groups.length; i++) {            
             taxTotal = taxTotal + ((groups[i].perc * groups[i].sum) / 100);  
          };
         return taxTotal;
     }; 
    }]);

If I want to access from my controller, it doesn´t work: I cannot get the variable taxTotals *Cannot read property 'length' of undefined As mentioned, in the view it works.

Filter Service

var taxTotal = $filter('sumFilter')($scope.groups);
console.log(taxTotal);

1 Comment

In your original code, I think you had $scope.groupArr - if I recall correctly. If $scope.groupArr is where you have your array populated, then pass this to your $filter function. i.e. $filter('sumFilter')($scope.groupArr);
0

Or use Map Reduce!

Controller

$scope.mappers = {
        tax: function(m){
            return group.sum * group.perc / 100.0;
        }
    }


$scope.sum = function(m){
        if($scope.groupsArr.length == 0) return;
        return $scope.groupsArr.map(m).reduce(function(p, c){
            return p + c;
        }) || 0;
};

HTML

<tr ng-repeat="group in groupsArr">                                         
    <td class="total-rows" ng-model="taxes">{{group.sum * group.perc / 100.0 | currency :""}}    </td>
</tr>
 <tr>
      <b> Tax Totals: </b> {{ sum(mappers.tax) }}
 </tr>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.