如何计算AngularJS中重复元素的总和?

时间:2016-04-25 15:34:04

标签: angularjs

我是AngularJS的新手并且在过去一周内遇到了问题。 我正在为不同的群体获得每周动态值。

我的目标是计算通过过滤器可以实现的每个组的值的总和,但是如果我更改sum的值,我无法弄清楚如何重新计算每周值。到目前为止我做了什么:

控制器:

app.controller("CalculationController", [
"$scope", "calculationService", function ($scope, calculationService) {
    getGroups();

    function getGroups() {
        calculationService.GetGroups().success(function(groups) {
            $scope.groups= groups;
            console.log(groups);
        }).error(function (error) {
            $scope.status = "Error Occured: " + error.message;
            console.log($scope.status);
        });
    }$scope.getTotal = function () {
        var groupTotal = []; 
        var total = 0; // hold the total for week
        var totalPerGroup = 0;
        var totalPerGroupArr = [[], []]; //array to store total values Per Group
        var groups = $scope.groups; //getting the data of groups in a variable.
        var totalCostForEachWeekPerMainGroup = [[], []]; //array to store total groups for each week            
        var totalGroupsCostPerGroup = []; //array to store the total cost per group
        totalCostPerMainGroup = 0; //sum of all groups per Main Group


        for (var i = 0; i < trps.length; i++) {
            total = 0;
            totalPerGroup = 0;

            for (var j = 0; j < groups[i].WeeklyValues.length; j++) {
                totalCostForEachWeekPerMainGroup[j][i] = groups[j].WeeklyValues[i].Cost;
                totalPerGroupArr[i][j] = groups[i].WeeklyValues[j].Cost;
                totalPerGroup += totalPerGroupArr[i][j];
                total += totalCostForEachWeekPerMainGroup[j][i];
                totalCostPerMainGroup += totalCostForEachWeekPerMainGroup[j][i];
            }
            groupTotal.push(total);
            totalGroupsCostPerGroup.push(totalPerGroup);
            $scope.totalGroupsCostPerMainGroup = totalCostPerMainGroup;
        }


        //Percentage calculation for each week                        
        return groupTotal;
    }
}]).filter('sumCalculation', function () {
return function (data, key) {
    if (typeof (data) === 'undefined' || typeof (key) === 'undefined') {
        return 0;
    }
    var total = 0;
    for (var i = 0; i < data.length; i++) {
        total += parseFloat(data[i][key]);
    }
    //$scope.sum = total;
    return total;
}});

查看:

<table class="table table-hover">
            <tr>
                <th>@Html.Label("Main Group")</th>
                <th>@Html.Label("Group")</th>
                <th ng-repeat="w in numberOfWeeks" ng-cloak>W{{w}}-2016</th>
                <th>@Html.Label("Total")</th>
            </tr>
            <tr ng-repeat="(k, v) in GroupTotalPerMainGroupPerWeek"> // coming from another service
                <td>{{k}}</td>
                <td></td>
                <td></td>
                <td ng-repeat="cost in getTotal()">
                    {{cost}}
                </td>
                <td>{{totalGroupsCostPerMainGroup}}</td>
            </tr>

            <tr ng-repeat="g in groups">
                <td></td>
                <td ng-cloak>{{g.GroupName}}</td>                    
                <td ng-repeat="c in g.WeeklyValues">
                    <input value="{{c.Cost}}" class="form-control" type="number" ng-model="c.Cost" ng-cloak/>
                </td>
                <td>
                    <input value="{{g.WeeklyValues|sumCalculation:'Cost'}}" class="form-control" string-to-number ng-model="Cost" type="number" ng-cloak/>
                </td>
            </tr>
        </table>

服务:

app.factory("CalculationService", ['$http', function ($http) {
var CalculationService= {};

CalculationService.GetGroups = function () {
    return $http.get("/Angular/GetGroups");
}

return CalculationService;}]);

我想要实现的目标:

  1. 计算每组每个值的总和

  2. 计算每周所有组的总和

  3. 计算所有组的总和
  4. 如果我更改sum的值,则应相应地更改每周值
  5. 如果我更改总值(例如1265),则应重新计算每组的总和(值),并重新计算每周值:
  6. 视图的例子如下:

    组名------ ------ Week1Value ------ Week2Value ------ Week3Value总和

    ------------------ 312 ------------- 328 --------- 625 ---- ------- 1265

    G1 -------------- 112 ------------- 113 --------- 300 ------- --- 525

    G2 -------------- 200 ------------- 215 --------- 325 ------- --- 740

0 个答案:

没有答案