我正在尝试在AngularJS中创建一个组的总和。我正在使用http将结果导入到html表中:
$http({method: 'GET', url: urlpurchasing}).success(function(data) {
$scope.purchasing = data;
})
我想创建一个名为“total stock”的新列,并为每个组添加所有“售出数量”,因此对于所有具有相同值的Desc的我想要的“已售出数量”将被添加。例如,底部的3个紫色行在“已售出总数”列中将具有“607”。
我尝试使用angular for-each遍历数据并添加每个数据,但这涉及创建第二个数组,主表中的任何类型的过滤器或更改都会更改索引并将其混合。感谢任何帮助。
修改
这是我到目前为止所做的事情(但总数每次都在增加:
$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;
})
这是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);
?>
答案 0 :(得分:1)
我不使用MySQL,但标准SQL应该在这里做到:
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
答案 1 :(得分:0)