rodrigo.rodriguez
rodrigo.rodriguez

Reputation: 298

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

Upvotes: 2

Views: 104

Answers (2)

Rafael Herscovici
Rafael Herscovici

Reputation: 17094

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

Upvotes: 2

Naga Sai A
Naga Sai A

Reputation: 10975

To achieve expected result , use below option

  1. Use scope variabled for filtered values

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

2.Use ng-keyup to get filtered values on input field

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

3. In Key up event, calculate total again with filtered values

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

Issue from your code: Calculating total on $scope.records which remains same always event with filter and without filter
Total is getting calculated on intial load with $scope.records and not with filter

Upvotes: 2

Related Questions