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

Calculation of the sum of repeating elements within a filter using the FILTER function of AngularJS

我有一个显示记录总数的列表,当过滤单个名称时它仍然显示记录总数而不是过滤记录的总数。 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>

结果:

带过滤器:

我的问题

要达到预期结果,请使用以下选项

  1. 对筛选值使用变量范围

    ng-repeat="x in filteredValues = (records | filter: model_filter)"

2.Use 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

计算总计初始负载

<!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>