Search code examples
htmlcssangularjsmeanjs

How To Total Sum Inside Of Array Values In Angularjs?


I am using MEAN stack in my application with AngularJS as my front-end. How to total sum inside of array values in angularjs , actually we have used ng-repeat inside of one more ng-repeat to fetch the value in table and I got the fetched value like 250 in Sprice then I'm expecting to totalsum the sprice value like as a ans= 250.

My Plunker.

  • We have used two ng-repeat to get the sprice values in table because sprice is in inside of the [array].

  • We need to calculate total sum of sprice of value in table.

  • We have filter functionality to calculate the totalsum of ng-module value if it is in without array.

  • Actually we dont' know how to use resultValue= in ng-repeat.

  • For example: sprice value is 250 in table means the totalsum value need to be in 250, if sprice value is 250 and 300 means expecting answer like 550.

  • Without array totalsum functionality: My Plunker

  • With array totalsum functionality: My Plunker

My Controller:

  .filter('sumOfValue', function () {
    return function (data, key) {
        debugger;
        if (angular.isUndefined(data) && angular.isUndefined(key))
            return 0;        
        var sum = 0;

        angular.forEach(data,function(v,k){
            sum = sum + parseFloat(v[key]);
        });        
        return sum.toFixed(2);
    }
})

My Data:-

    $scope.orders = [
{
"_id": "5836b64083d9ce0f0078eae8",
"user": {
"_id": "579bdf6123f37f0e00a40deb",
"displayName": "Table 1"
},
"__v": 8,
"total": "1824",
"ordercar": [],
"orderfood": [
{
"qty": "1",
"confirm": "placed",
"sprice": 250,
"price": 250,
"customise": "With Onion,Without Onion",
"name": "Baasha Pizza"
}
],
"phone": null,
"order_source": "",
"comment": "",
"payment_mode": "",
"nop": null,
"rating": null,
"bill": false,
"complete": false,
"laundry": false,
"clean": false,
"roomservice": false,
"napkin": false,
"waiter": false,
"water": false,
"name": "fgg",
"created": "2016-11-24T09:43:28.413Z",
"isCurrentUserOwner": true
}]

My Html:-

<table ng-table="tableParams"  class="table table-bordered ">
    <thead>
    <tr>

        <th rowspan="2">s.no</th>
        <th rowspan="2"> sprice </th>
        </tr>
</thead>
        <tr ng-repeat="order in orders">


            <td>{{$index + 1}}</td>
            <td ng-repeat="ram in order.orderfood">{{ram.sprice }}</td>


           </tr>
           <tr>
             <td>sum</td>

             <td>{{resultValue | sumOfValue:'sprice'}}</td>


           </tr>
          </table>

*We expecting for total sum in array values.

Used ng-repeat:

<tr ng-repeat="order in orders">


            <td>{{$index + 1}}</td>
            <td ng-repeat="ram in order.orderfood">{{ram.sprice }}</td>

Solution

  • ng-repeat is not looping properly .. I fixed it .. Check below snippet and also i added one more array to it.......... let me know..

    var app = angular.module('plunker', []);
    
    
    app.filter('sumOfValue', function () {
        return function (data, key) {
            debugger;
            if (angular.isUndefined(data) && angular.isUndefined(key))
                return 0;        
            var sum = 0;
            
            angular.forEach(data,function(v,k){
                sum = sum + parseFloat(v[key]);
            });        
            return sum.toFixed(2);
        }
    }).controller('MainCtrl', function($scope) {
      $scope.orders = [
    {
    "_id": "5836b64083d9ce0f0078eae8",
    "user": {
    "_id": "579bdf6123f37f0e00a40deb",
    "displayName": "Table 1"
    },
    "__v": 8,
    "total": "1824",
    "ordercar": [],
    "orderfood": [
    {
    "qty": "1",
    "confirm": "placed",
    "sprice": 250,
    "price": 250,
    "customise": "With Onion,Without Onion",
    "name": "Baasha Pizza"
    },
    {
    "qty": "2",
    "confirm": "placed",
    "sprice": 250,
    "price": 250,
    "customise": "With Onion,Without Onion",
    "name": "Baasha Pizza"
    }
    ],
    "phone": null,
    "order_source": "",
    "comment": "",
    "payment_mode": "",
    "nop": null,
    "rating": null,
    "bill": false,
    "complete": false,
    "laundry": false,
    "clean": false,
    "roomservice": false,
    "napkin": false,
    "waiter": false,
    "water": false,
    "name": "fgg",
    "created": "2016-11-24T09:43:28.413Z",
    "isCurrentUserOwner": true
    }]
    
    });
    body {
        font-size: 14px;
    }
    
    table
    {
        border-collapse:collapse;
    }
    table, td, th
    {
        border:1px solid black;
    }
    td{
        padding: 2px;
    }
    .servicetaxinclusivetrue:before{
      color: green!important;
      content: "\f00c";
    }
    .servicetaxinclusivefalse:before{
      color: red!important;
      content: "\f00d";
    }
    .servicetaxexclusivetrue:before{
      color: green!important;
      content: "\f00c";
    }
    .servicetaxexclusivefalse:before{
      color: red!important;
      content: "\f00d";
    }
    <!DOCTYPE html>
    <html ng-app="plunker">
      <head>
        <meta charset="utf-8" />
        <title>AngularJS Plunker</title>
        <script>document.write('<base href="' + document.location + '" />');</script>
        <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
        <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    
      </head>
    
      <body ng-controller="MainCtrl">
        
        
        <table ng-table="tableParams"  class="table table-bordered ">
            <thead>
            <tr>
                
                <th rowspan="2">s.no</th>
                <th rowspan="2"> sprice </th>
                </tr>
        </thead>
    	<tbody ng-repeat="order in orders">
                <tr ng-repeat="ram in resultValue=(order.orderfood)">
                  <td>{{$index + 1}}</td>
                    <td >{{ram.sprice }}</td>
                  </tr>
                   <tr>
                    <td>sum</td>
                     <td>{{resultValue | sumOfValue:'sprice'}}</td>
              
                   </tr>
    			   </tbody>
                  </table>
    </body>
    
    </html>