如何使用angularjs计算ng-repeat中相同名称的总和

时间:2017-01-09 08:51:54

标签: angularjs ionic-framework

这是app.js

AllServices.teamAllDataFunction1(User)
        .then(function(response) {
            $scope.User.data=response.data['DeatilTeam'];
            console.log($scope.User.data);
            var sum = 0;
            for (var i = 0; i < ($scope.User.data).length; i++) {
                sum += parseInt($scope.User.data[i].dp_inst_pending) || 0;
            }
});

这里得到所有数据,其中有这么多记录具有相同的名称但不同的待定金额,所以我想只显示单个名称和那里的笔记金额的总和,以便我可以在单行显示它。我没有得到如何实现它...请帮助我..

这里是html,

<table ng-table>
                <tr>
                    <th>advisor_name</th>
                    <th>totalpending</th>
                </tr>

                <tr ng-repeat="data in User.data" ng-if="data.dp_inst_pending && data.dp_inst_pending == 0">
                    <td>{{data.AdvisorName}}</td>
                    <td>{{data.dp_inst_pending}}</td>  
                </tr>
            </table>

enter image description here

以这种方式获取数据

1 个答案:

答案 0 :(得分:0)

代码几乎没有一般性问题,让我们先解决这个问题。

首先,假设您的data.dp_inst_pending是Integer,您已直接在ng-if中使用它。在这种情况下,如果某人实际上没有待处理的金额,则不会显示。

其次,您要计算具有相同名称的帐户的待处理金额总和。为此你必须运行一个双循环。一个用于检查名称,另一个用于在名称相同时添加待处理金额。我已经创建了一个相同的样本,我认为满足您的要求。请仔细检查并根据需要进行优化。

var myApp = angular.module("myApp", []);
myApp.controller("MyController", ['$scope',
  function($scope) {
    $scope.User = {};
    $scope.User.data = [{
      "advisor_name": "Test1",
      "dp_inst_pending": 300
    }, {
      "advisor_name": "Test2",
      "dp_inst_pending": 0
    }, {
      "advisor_name": "Test1",
      "dp_inst_pending": 500
    }, {
      "advisor_name": "Test2",
      "dp_inst_pending": 600
    }, {
      "advisor_name": "Test1",
      "dp_inst_pending": 15
    }, {
      "advisor_name": "Test2",
      "dp_inst_pending": 150
    }, {
      "advisor_name": "Test3",
      "dp_inst_pending": 30
    }];
    $scope.User.summedData = [];
    var sums = [];
    var advisorNames = [];


    for (i = 0; i < $scope.User.data.length; i++) {
      var key = $scope.User.data[i].advisor_name;
      if (advisorNames.indexOf(key) >= 0) {} else {
        var sumOfKey = 0;
        for (j = 0; j < $scope.User.data.length; j++) {
          if ($scope.User.data[j].advisor_name == key) {
            sumOfKey += $scope.User.data[j].dp_inst_pending;
          }
        }
        sums.push(sumOfKey);
        advisorNames.push(key);
      }
    }
    $scope.Test = {};
    $scope.Test.data = [];
    for (k = 0; k < sums.length; k++) {
      $scope.Test.data[k] = {
        "advisor_name": advisorNames[k],
        "dp_inst_pending": sums[k]
      }
    }
    console.log(sums);
    console.log(advisorNames);
    console.log($scope.Test.data);
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="MyController">
    <table ng-table>
      <tr>
        <th>advisor_name</th>
        <th>totalpending</th>
      </tr>

      <tr ng-repeat="data in Test.data">
        <td>{{data.advisor_name}}</td>
        <td>{{data.dp_inst_pending}}</td>
      </tr>
    </table>

  </div>
</div>

我没有优化它,因为它应该由你完成。