2016-10-08 32 views
0

我試圖計算表中有重複項目的總數。我能總結出每獲得每個項目的子總,但得到的所有項目的盛大之和問題用angularjs總結重複的項目

.controller('reciept_ctrl',['$scope','$http','$state','$stateParams','$window',function($scope,$http,$state,$stateParams,$window){ 
    $scope.shop_id=localStorage.getItem("shop_id"); 
    $scope.payment_id=localStorage.getItem("payment_id"); 
    $http.get('http://localhost/sales/reciept.php?payment_id='+$stateParams.payment_id + "&shop_id="+$scope.shop_id).success(function(data){ 
     console.log(JSON.stringify(data)); 
     $scope.reciept=data; 

     }); 

HTML

<div ng-repeat="item in reciept"> 
     <table width="100%" border="0" cellspacing="4" cellpadding="4"> 
      <tr> 
      <td width="50%"><div align="left">{{item.item}}</div></td> 
      <td width="16%"><div align="center">{{item.price}}</div></td> 
      <td width="12%"><div align="center">{{item.quantity}}</div></td> 
      <td width="20%"><div align="center">GH¢ {{item.quantity*item.price}}</div></td> 
      </tr> 
      <tr> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
      <td colspan="2">&nbsp;</td> 
      </tr> 
     </table> 
     <p> 

     </div> 

<table width="100%" border="0" cellspacing="1" cellpadding="1"> 
    <tr> 
    <td>Grand Total:</td> 
    </tr> 
</table> 
+0

是有必要的,你將不得不計算的標記,而不是JS的大小? – Siddharth

回答

0

你可以有一個函數來計算盛大總,

$scope.getTotal = function(type) { 
     var total = 0; 
     angular.forEach($scope.items, function(el) { 
      total += el[type]; 
     }); 
     return total; 
    }; 

DEMO

0

可以初始化總價值像

<td width="16%" ng-init="total=total+item.price"><div align="center">{{item.price}}</div></td> 

,並在總計只是把

<td>Grand Total:{{total}}</td>