Currently, I am working on calculating the total sum of a group in AngularJS. My approach involves using http to retrieve results and display them in an HTML table:
$http({method: 'GET', url: urlpurchasing}).success(function(data) {
$scope.purchasing = data;
})
As shown in the link below:
I aim to create a new column named "total stock" where the "Quantity sold" for each group will be added up. Specifically, I want to aggregate the "quantity sold" values for groups that share the same description. For instance, the three purple rows at the end should have a total of "607" in their respective "total sold" column.
I attempted to iterate through the data using an angular foreach loop and keep track of the cumulative total. However, this method requires creating a secondary array which can lead to discrepancies when filtering or altering the main table. Any help on this issue would be much appreciated.
Edit:
Below is my current solution, wherein the totals are repeatedly increasing:
$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;
})
This PHP script retrieves data from the database:
<?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);
?>