AngularJS行的总和ng-repeat

时间:2014-05-24 08:35:21

标签: javascript angularjs

我使用ng-repeat动态添加表格中的行,来自数组。

现在我想获得每行所有总和的总和(group.sum * group.perc / 100.0)。我需要在变量中,因为我需要这个值进行进一步的计算。谢谢

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 个答案:

答案 0 :(得分:8)

创建过滤器:

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

使用$ filter服务:

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

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

在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>

答案 1 :(得分:0)

来自pixelbits的答案后更新

感谢pixelbits。这是我的过滤器,在视图中完美运行。

<强> 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>

过滤

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;
     }; 
    }]);

如果我想从我的控制器访问,它不起作用:我无法获取变量taxTotals *无法读取未定义的属性“长度” 如上所述,在视图中它起作用。

过滤服务

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

答案 2 :(得分:0)

添加最佳答案...我在我非常庞大的表格中使用过滤器,因此它是如何使用动态过滤器实现的。

过滤器

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;
}

});

HTML中的

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

答案 3 :(得分:0)

或使用Map Reduce!

<强>控制器

$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>