DRK DRK - 3 months ago 12
AngularJS Question

how to get table price total in angular

I created code for the get total. But It will not adding value. That all values will show as a one line. What is the wrong in this code.

ProductController.js

$scope.getTotal = function(type) {
var total = 0;
angular.forEach($scope.products, function(el) {
total += el[type];
});
return total;
};


cart.html

<table class="cartdata">
<tr>
<th class="a">Name</th>
<th class="a">Price</th>
<th class="a">Post Date</th>
<th class="a">Remove Item</th>
</tr>
<tr ng-repeat="produ in products track by $index" ng-init="setTotals(produ)">
<td class='b'>{{produ.post_title}}</td>
<td class="b">{{produ.ID | currency}}</td>
<td class="b">
<a ng-click="delete(post.id, $index)" class="btn btn-danger"></a>
</td>
</tr>

<td>Total{{getTotal('ID') | currency }}</td>

</table>


Cart view

-------------------------
| Name | Price |
|------------------------
|Product 1 | $20.00 |
|Product 2 | $35.00 |
|Product 3 | $10.00 |
|-----------------------|

Total = $ 203,510.00

Answer

Check this - fiddle

$scope.getTotal = function(type) {
    var total = 0;
    angular.forEach($scope.products, function(el) {
        total += parseFloat(el[type]);
    });
    return total;
 };