2016-11-19 73 views
0

基於以下table我需要獲取所有小計的總和。AngularJS:從多個對象中總結嵌套對象

我試圖使用相同的sumByKey過濾器,但我沒有工作。 另外,所有小計的總和必須基於過濾器的結果,這意味着,如果我們有兩個結果(兩個類別),則小計的總和必須基於這些對象。任何想法?

HTML

<table border="1"> 
    <thead> 
    <tr> 
     <th>#</th> 
     <th>Category</th> 
     <th>Products with quantities</th> 
     <th>Subtotal of quantities</th> 
    </tr> 
    </thead> 
    <tbody align="center"> 
    <tr data-ng-repeat="category in categories | filter:search"> 
     <td>{{$index+1}}</td> 
     <td>{{category.name}}</td> 
     <td>{{category.products}}</td> 
     <td>{{category.products | sumByKey:'quantity'}}</td> 
    </tr> 
    </tbody> 
    <thead align="right"> 
    <tr> 
     <td colspan="3"><strong>Total</strong></td> 
     <td></td> 
    </tr> 
    </thead> 
</table> 

angularjs

var app = angular.module("app", []); 
app.controller("controllerApp", function($scope, $http){ 
    $http.get("categories.json").success(function(data) { 
     $scope.categories = data; 
    }); 
}); 

app.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; 
} 
}); 

回答

0

它也可以用角度來完成。

實施例:http://plnkr.co/edit/zhTtoOjW7J1oumktlvFP?p=preview

HTML:

<table border="1"> 
     <thead> 
     <tr> 
      <th>#</th> 
      <th>Category</th> 
      <th>Products with quantities</th> 
      <th>Subtotal of quantities</th> 
     </tr> 
     </thead> 
     <tbody align="center"> 
     <tr data-ng-repeat="category in filterValues=(categories | filter:search)"> 
      <td>{{$index+1}}</td> 
      <td>{{category.name}}</td> 
      <td>{{category.products}}</td> 
      <td>{{category.products | sumByKey:'quantity'}}</td> 
     </tr> 
     </tbody> 
     <thead align="right"> 
     <tr> 
      <td colspan="3"><strong>Total</strong></td> 
      <td>{{filterValues | sumOfValue:'quantity'}}</td> 
     </tr> 
     </thead> 
    </table> 

JS:

// Code goes here 
var app = angular.module("app", []); 
app.controller("controllerApp", function($scope, $http) { 
    $http.get("categories.json").success(function(data) { 
    $scope.categories = data; 
    }); 
}); 

app.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; 
    } 
}); 

app.filter('sumOfValue', function() { 
    return function(data, key) { 
    if (angular.isUndefined(data) || angular.isUndefined(key)) { 
     return 0; 
    } 
    var sum = 0; 
    for (var i = 0; i < data.length; i++) { 
     var value = data[i]; 
     if (!angular.isUndefined(value)) { 
     for (var j = 0; j < value.products.length; j++) { 
      sum += parseInt(value.products[j][key]); 
     } 
     } 
    } 
    return sum; 
    } 
}); 
1

未必Angular解決方案。但你也可以通過純粹的JavaScript得到總數。

通過保持一個total$scope varaible像這裏面你controller

 $scope.total = getTotal(data); 
    function getTotal(data){ 
     var total = 0; 
     data.forEach(function(item){ 
     item.products.forEach(function(product){ 
      total += product.quantity; 
     }) 
     }); 
     return total; 
    } 

Here是更新Plunker

+0

當我過濾類別 '總' 必須根據經濾波的對象。嘗試使用過濾器以查看總數。任何想法?順便謝謝你的回答。 – wilson