Search code examples
javascriptangularjsangularjs-ng-repeatangularjs-filter

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


I have a list that shows the total of the records, when filtering a single name it still shows me the total of the records and not the total of the filtered record. 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>

RESULT:

enter image description here

WITH FILTER:

enter image description here

MY PROBLEM

enter image description here


Solution

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