R. Mani Selvam R. Mani Selvam - 1 month ago 5
AngularJS Question

How Do I Sum the Total Of The Column Values If some Row Value Is Not Available in Angularjs?

I am using MEAN stack in my application with AngularJS as my front-end. How to

total sum
column values, if some row value is not available in table, actually I got the
total sum value
but sum ~row value is not available~ the totalsum is showing like
NaN
...My Plunker For Example :- in table all row value is available means
amt
values total sum answer I got 5775.30, then in table if some row value is not available means
amount payment
values total sum showing like
NaN
, Expecting answer like
200
, If any one knows the solution help to us thanks....


  • In table column all values is available we got exact answer and total sum also.

  • But in table, a single row value of
    amount payment
    is not available means the total sum values is showing like
    NaN
    ... so wt we expecting if the some row value is not available the total sum should be calculate rest of the values ...expecting
    amount payment
    answer like 200...please look at 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 Html:-

<td >{{mani.amt}}</td>

<td >{{mani.amount_payment }}</td>


My Data:-

{
"_id": "5816f4fad0be79f809519f98",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"created": "2016-10-31T07:38:34.999Z",
"remarks": "-",
"status": "pending",
"amt": "1925.10",
"cheque_currency": "Rs",
"cheque_value": "300",
"amount_payment": "1,925.10",
"debitnote_no_payment": "3",
"supplier_name": "karikalan",
"buyer_name": "Manidesigns"
},

{
"_id": "5816f4fad0be79f809519f98",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"created": "2016-10-31T07:38:34.999Z",
"remarks": "-",
"status": "pending",
"amt": "1925.10",
"cheque_currency": "Rs",
"cheque_value": "300",
"amount_payment": "1,925.10",
"debitnote_no_payment": "3",
"supplier_name": "karikalan",
"buyer_name": "Manidesigns"
},

{
"_id": "5816f4fad0be79f809519f98",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"created": "2016-10-31T07:38:34.999Z",
"remarks": "-",
"status": "pending",
"amt": "1925.10",
"cheque_currency": "Rs",
"cheque_value": "300",
"amount_payment": "",
"debitnote_no_payment": "3",
"supplier_name": "karikalan",
"buyer_name": "Manidesigns"
},


I have created Plunker for reference:- Plunker

Answer

Changed some code of your: Check for isNAN function of javascript

angular.forEach(data, function(v, k) {

  var keyval;
  if(isNaN(parseFloat(v[key])))
  keyval=0;
  else
  keyval=parseFloat(v[key]);
  sum = sum + keyval;
});
Comments