繁体   English   中英

AngularJS行的总和ng-repeat

[英]AngularJS sum of rows ng-repeat

我使用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>

脚本

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

创建一个过滤器:

 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>

来自像素位的答案后的更新

感谢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);

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

过滤器

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>

或者使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM