Gaz Smith
Gaz Smith

Reputation: 1108

AngularJS grouping values and adding them together in ng-repeat

I'm trying to create a total sum of a group in AngularJS. I'm using http to get the results into a html table :

    $http({method: 'GET', url: urlpurchasing}).success(function(data) {

               $scope.purchasing = data;

             })

Which gives the below result : enter image description here

I want to make a new column called "total stock" and add all of the "Quantity sold" for each group, so for all which have a Desc of the same value i want there "quantity sold" to be added up. For example, the 3 purple rows at the bottom would have "607" in there "total sold" column.

I tried to loop through the data with an angular for-each and add each one up but this involves creating a second array and any kind of filter or change in the main table changes the indexes and it mixes up. Appreciate any assistance.

edit

This is what i have so far (but the totals are incrementing each time :

  $http({method: 'GET', url: urlpurchasing}).success(function(data) {


                                var t = 0;

                                 angular.forEach(data, function(obj){


                                    if($scope.code == obj.GroupCode){

                                       }
                                       else
                                       {
                                        $scope.code = obj.GroupCode;
                                        t = 0;

                                       }

                                     t = (t + parseInt(obj.QuantitySold));
                                     obj.total = t;


                                  });

                                    $scope.purchasing = data;


                     })

Here is the PHP :

<?php

require_once('sqlconnect.php');
$sqlQuery = "select StockCode,Description,QuantityInStock,QuantitySold,NetAmountSold,GroupCode,color from purchasing order by Description desc"; 
$result = $unity_connection->query($sqlQuery);

$json1 = array();
while($rows = mysqli_fetch_assoc($result)){
    $json1[] = $rows;
}   

    echo json_encode($json1);

?>

Upvotes: 0

Views: 180

Answers (2)

Mike Feltman
Mike Feltman

Reputation: 5176

I don't use MySQL, but standard SQL should do the trick here:

SELECT
    purchasing.stockcode,
    purchasing.description,
    purchasing.quantityinstock,
    purchasing.quantitysold,
    purchasing.netamountsold,
    purchasing.groupcode,
    purchasing.color,
    desc_summary.totalstock
FROM
    purchasing join (select description, sum(quantitysold) as TotalStock from purchasing group by descrption) desc_summary on purchasing.description = desc_summary.description 
ORDER BY purchasing.description DESC

Upvotes: 1

anoop
anoop

Reputation: 3822

You can achieve this by multiple ways, either to create extra variable for total in your controller scope or directly in your data\json as well., or by extending $scope.purchansing

OR, if you don't want any further $scope property, then you can invoke a function at run time to calculate the total for each group..

See this fiddle in which I tried with run time function which checks for group total per group.

Updated fiddle to extend your scope data in controller.

Upvotes: 0

Related Questions