使用AngularJS的FILTER函数计算过滤器中重复元素的总和

时间:2019-06-10 14:12:57

标签: javascript angularjs angularjs-ng-repeat angularjs-filter

我有一个显示记录总数的列表,当过滤单个名称时,它仍然显示记录总数而不是过滤记录的总数。 TRY HERE

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
  name:
<input type="text" ng-model="model_filter">
  <hr/>
    <table ng-controller="myCtrl" border="1">
        <tr ng-repeat="x in records | filter: model_filter">
            <td>{{x.Name}}</td>
            <td>{{x.Money | currency}}</td>
        </tr>
        <tr>
            <td>TOTAL</td>
            <td>{{Total | currency}}</td>
        </tr>
    </table>
    <script>
        var app = angular.module("myApp", []);
        app.controller("myCtrl", function ($scope) {
            $scope.records = [
                {
                    "Name": "Rodrigo",
                    "Money": 10
                },
                {
                    "Name": "Rodrigo",
                    "Money": 25
                },
                {
                    "Name": "Arnodl",
                    "Money": 15
                },
                {
                    "Name": "Carlos",
                    "Money": 5
                }
            ]
            $scope.Total = 0;
            for (var i = 0; i < $scope.records.length; i++) {
                $scope.Total += $scope.records[i].Money;
            }

        });
    </script>

</body>
</html>

结果:

enter image description here

有过滤器:

enter image description here

我的问题

enter image description here

2 个答案:

答案 0 :(得分:1)

要获得预期结果,请使用以下选项

  1. 将变量的作用域用于过滤后的值

    ng-repeat =“ x已过滤值=(记录|过滤器:model_filter)”

2。使用ng-keyup在输入字段上获取过滤后的值

<input type="text" ng-model="model_filter" ng-keyup="calcTotal($event)">

3。在Key up事件中,使用过滤后的值再次计算总数

$scope.calcTotal = function(e){
            $scope.Total = 0;
            for (var i = 0; i < $scope.filtered.length; i++) {
                     $scope.Total += $scope.filtered[i].Money;

            }
          }

codepen-https://codepen.io/nagasai/pen/KjPrvr

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
  name:
<input type="text" ng-model="model_filter" ng-keyup="calcTotal($event)">
  <hr/>
    <table border="1">
        <tr ng-repeat="x in filteredValues = (records | filter: model_filter)">
            <td>{{x.Name}}</td>
            <td>{{x.Money | currency}}</td>
        </tr>
        <tr>
            <td>TOTAL</td>
            <td>{{Total | currency}}</td>
        </tr>
    </table>
    <script>
        var app = angular.module("myApp", []);
        app.controller("myCtrl", function ($scope) {
          $scope.calcTotal = function(e){
            $scope.Total = 0;
            for (var i = 0; i < $scope.filteredValues.length; i++) {
                     $scope.Total += $scope.filteredValues[i].Money;
              
            }
          }
            $scope.records = [
                {
                    "Name": "Rodrigo",
                    "Money": 10
                },
                {
                    "Name": "Rodrigo",
                    "Money": 25
                },
                {
                    "Name": "Arnodl",
                    "Money": 15
                },
                {
                    "Name": "Carlos",
                    "Money": 5
                }
            ]
            $scope.Total = 0;
            for (var i = 0; i < $scope.records.length; i++) {
                     $scope.Total += $scope.records[i].Money;
              
            }
        });
    </script>

</body>
</html>

来自您的代码的问题: 计算$ scope.records上的总数,始终保持相同的事件(使用过滤器和不使用过滤器
) 总计是通过$ scope.records而不是filter来计算初始负载的

答案 1 :(得分:1)

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp">
  name:
  <input type="text" ng-model="model_filter">
  <hr/>
  <table ng-controller="myCtrl" border="1">
    <tr ng-repeat="x in records | filter: model_filter">
      <td>{{x.Name}}</td>
      <td>{{x.Money | currency}}</td>
    </tr>
    <tr>
      <th>TOTAL</th>
      <td>{{Total()}}</td>
    </tr>
    <tr>
      <th>TotalFiltered</th>
      <td>{{ TotalFiltered() }}</td>
    </tr>
  </table>
  <script>
    var app = angular.module("myApp", []);
    app.controller("myCtrl", function($scope) {
      $scope.records = [{
          "Name": "Rodrigo",
          "Money": 10
        },
        {
          "Name": "Rodrigo",
          "Money": 25
        },
        {
          "Name": "Arnodl",
          "Money": 15
        },
        {
          "Name": "Carlos",
          "Money": 5
        }
      ]
      $scope.Total = () => $scope.records.reduce((total, b) => total + b.Money, 0);
      $scope.TotalFiltered = () => {
        return $scope.records.reduce((total, b) => {
          if ($scope.model_filter && b.Name.toUpperCase().includes($scope.model_filter.toUpperCase())) { return total + b.Money; }
          else { return total; }
        }, 0);
      }
    });
  </script>

</body>

</html>