0

I am using MEAN stack in my application with AngularJS as my front-end. How to total sum two values in angularjs , actually we have two tables and I got the total sum values like 124.10 in convertion rate then I'M expecting to calculate convertion rate totalsum values like A + B as a 124.10 + 124.10 answer = 248.20...My Plunker.

  • We have two table so I want to calculate first table convertion rate total sum value and second table convertionrate totalsum value

  • For exmaple:- fisrt table convertion rate is 124.10, and second table convertion rate is 124.10 , we need to calculate these to value in third table like A + B answer should be 248.20.

  • I have given the plunker as reference plunker please any one knows the solution help us.

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.sryarndebitnote = [
{
"_id": "57ac1b6d82e1c5940ac3c730",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"created": "2016-08-11T06:30:05.118Z",
"shipment_id": "57ac19b982e1c5940ac3c72f",
"conversion_rate": "62.04",
"invoice_value_fob_currency": "Rs",
"invoice_value_fob": "300.231",
"invoice_quantity_unit": "KG",
"invoice_quantity": "37",
"invoice_date": "2016-08-17",
"supplier_name": "Msd",
"buyer_name": "Mani selvam .R"
},

{
"_id": "57b5af69df0475401f644b2e",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"created": "2016-08-18T12:51:53.671Z",
"shipment_id": "57b5af5bdf0475401f644b2d",
"conversion_rate": "62.06",
"exclusive": true,
"invoice_value_fob": "400.343",
"invoice_quantity": "97",
"supplier_name": "Msd",
"buyer_name": "Mani selvam .R"
},]

My Html:-

<tr ng-repeat="mani in resultValue=(sryarndebitnote)"> 
     <td >{{$index + 1}}</td>
       <td >{{(resultValue | sumOfValue:'conversion_rate' * 1) + (resultValue | sumOfValue:'conversion_rate' *1)}}</td>
         </tr>
     <tr>
        <td>sum</td>
            <td>B = {{resultValue | sumOfValue:'conversion_rate'}}</td>

         </tr>
  • Third table should be a+b calculating values and final total sum of C value like A+B value 248.20 , so the c value total sum is 496.40.

  • c total sum value `496.40.

R. Mani Selvam
  • 320
  • 8
  • 36

2 Answers2

0
{{(resultValue | sumOfValue:'conversion_rate' * 1) + (resultValue | sumOfValue:'conversion_rate' *1)}}

Using this kind of tricks to force integer summation is discouraged and you should not use them in sensitive applications.

That being said; You have to actually force the result from filter to be an integer. e.g:

   {{(resultValue | sumOfValue:'conversion_rate')*1 + (resultValue | sumOfValue:'conversion_rate')*1}}

But you really should come up with a more robust approach in this situtation. Even wrapping parseInt function in your controller does the trick.

For further reading:

How to parseInt in Angular.js

Community
  • 1
  • 1
JuniorDev
  • 1,170
  • 2
  • 9
  • 19
  • hey thanks for your valuable comment and answer, we got `A+B` answer like `248.2`as well , then need totalsum of A+B , for example :- `C` value like 496.4.... any idea or solution ? please....then please update plunker as well to the solution as well...updated plunker:- http://plnkr.co/edit/RWKE2wESbFhJ4p4WreFZ?p=preview ....if we got the total sum of A+B i will give you the tick mark as well...thanks a lot.. – R. Mani Selvam Oct 01 '16 at 10:24
0

Change this:

 <td >{{(resultValue | sumOfValue:'conversion_rate' * 1) + (resultValue | sumOfValue:'conversion_rate' *1)}}</td>

To this:

 <td>{{((resultValue | sumOfValue:'conversion_rate')*1) + ((resultValue | sumOfValue:'conversion_rate' )*1)}}</td>

The problem is parenthesis.

sylvain1264
  • 855
  • 2
  • 8
  • 23
mgündüz
  • 16
  • 3
  • Hey thanks for your valuable comment and answer, we got A+B answer like 248.2 as well , then need totalsum of A+B , for example :- We Expecting C value like 496.4.... any idea or solution ? please....then please update plunker as well to the solution as well...updated plunker:- http://plnkr.co/edit/7N202S3BldCxZuADLOnY?p=preview ....if we got the total sum of A+B i will give you the tick mark as well...thanks a lot.. – R. Mani Selvam Oct 01 '16 at 11:57
  • thanks for your answer it's working perfectly... thanks for your help... – R. Mani Selvam Oct 03 '16 at 07:46