如果某些行值在 Angularjs 中不可用,如何求和列值的总和?

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

我在我的应用程序中使用 MEAN 堆栈,AngularJS 作为我的前端。如何 total sum 列值,如果某些行值在 table 中不可用,实际上我得到了 total sum value 但求和~行值不可用~总和显示为 [=16] =]...My Plunker 例如 :- 在 table 中所有行值都可用意味着 amt 值总和答案我得到 5775.30,然后在 table 中,如果某些行值不可用,则意味着 amount payment 值总和显示为 NaN,期望答案为 200,如果有人知道解决方案对我们有帮助,谢谢。 ...

我的控制器:-

    .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);
    }
})

我的Html:-

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

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

我的数据:-

        {
    "_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"
},

我创建了 Plunker 以供参考:- Plunker

如果值不是有效数字,方法 parseFloat() 将 return NaN。所以你得到的结果是 NaN。跳过所有无效值。下面的例子。希望这有帮助。

angular.forEach(data, function(v,k){
           var value = parseFloat(v[key]);
           if(isNaN(value)) {
             return;
           }
           sum = sum + value; 
        }); 

更改了您的一些代码:检查 javascript

的 isNAN 函数

angular.forEach(数据,函数(v, k) {

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