3

I am using MEAN stack in my application with AngularJS as my front-end. I am trying to get total sum of my subdocuments but I did not get the desired result. my fiddle

The result I got

Summary:
1000
100
50
100
100
Total:undefined

The result I'm expecting

Summary:
1000
100
50
100
100
Total:1350

HTML

<ul>
    <li ng-repeat="mani in items">
      <p ng-repeat ="rohit in mani.colorshades ">
      {{rohit.order_quantity}}
      </p>
    </li>
    <p class="length">Total:{{items.length}}</p>
</ul>

Controller

$scope.items = [{
"_id": "56f91708b7d0d40b0036bc09",
"colorshades": [
    {
    "_id": "56f9177fb7d0d40b0036bc0c",
    "order_quantity": "1000",
    },
    {
    "_id": "56f9177fb7d0d40b0036bc0b",
    "order_quantity": "100",
    },
    {
    "_id": "56f919d7b7d0d40b0036bc13",
    "order_quantity": "50",
    }]
},
{
"_id": "56f367e6a7d3730b008d296a",
"colorshades": [
    {
      "_id": "56f3680ba7d3730b008d296c",
      "order_quantity": "100",
    }
]
},
{
"_id": "56e7af485b15b20b00cad881",
"colorshades": [
    {
    "_id": "56e7af7b5b15b20b00cad882",
    "order_quantity": "100",
    }
]
}];

$scope.getTotals = function () {
    var total = 0;
    for (var i = 0; i < $scope.item.colorshades.length; i++) {
        var item = $scope.item.colorshades[i];
        total += (item.order_quantity);
    }
    return total;
};

my fiddle

ekad
  • 14,436
  • 26
  • 44
  • 46
  • HTML is used to display content and interact, JS is used to execute code. You need to execute code in JS to calculate a sum, maybe you should start from a JS basic guide – Naigel Mar 30 '16 at 07:03
  • have updated my controller and tried to get the total –  Mar 30 '16 at 07:50
  • well done! Now display the result of your function, so change `{{items.length}}` with `{{getTotals()}}` – Naigel Mar 30 '16 at 08:10
  • i have but not able to get the answer so pls look at my fiddle... –  Mar 30 '16 at 08:14
  • Sorry but it seems you don't really know how to code in JS. You have a `$scope.items` array, inside you have objects containing array of objects. You need at least 2 cycle to get that sum, but you need to know how to cycle arrays and get properties correctly... You should really start from the basics or you won't be able to achieve your result (: – Naigel Mar 30 '16 at 08:45

3 Answers3

2

You have a two level loops, the code should be like that (don't forget to cast the strings by parseInt otherwise you will have a concatenation) :

$scope.getTotals = function () {
var total = 0;
for (var i = 0; i < $scope.items.length; i++) {

    for (var j = 0; j < $scope.items[i].colorshades.length; j++) {
      total += parseInt(($scope.items[i].colorshades[j].order_quantity));
    }
}
return total;
};
Brahim LAMJAGUAR
  • 1,254
  • 5
  • 19
  • 28
0

There is no direct method available to calculate total order quantity. You need to have a controller method to calculate it and html can point it to that method or $scope variable in which total quantity is stored

Sagar
  • 106
  • 6
0

That should work:

$scope.getTotals = function () {
var total = 0;
for(var i = 0; i < $scope.items.length; i++) {
  var item = $scope.items[i].colorshades;
  for(var j = 0; j < item.length; j++) {
    total = total + parseInt(item[j].order_quantity);
   }
  }
  return total;
 };
}