您好我正在使用angular和php构建项目。我有一个"选择"我可以选择的选项,它向我展示了所有"类别"我需要,我需要选择一个。然后它可以计算产品的数量"当我选择"类别"。但是当我选择另一个"类别"总数没有重置为0再次计数它只是更重要。有人可以帮忙吗?
这是我的代码:
HTML:
<select ng-model="stockReport.selectedOption"
ng-change="computeTotal()" ng-options = "item.category_name for item in stockReport |
unique:'category_name'">
<option value="">בחר קטגוריה</option>
</select>
<div class="table-responsive">
<table class="customer-list table table-striped" >
<thead>
<tr >
<th class="Column-Header">קטגוריה</th>
<th class="Column-Header">קוד מוצר</th>
<th class="Column-Header">שם מוצר</th>
<th class="Column-Header">תיאור מוצר</th>
<th class="Column-Header">כמות במלאי</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in stockReport" ng-if = "item.category_name == stockReport.selectedOption.category_name"
ng-init="setTotals(item)">
<td>{{item.category_name}}</td>
<td>{{item.stock_id}}</td>
<td>{{item.product_name}}</td>
<td>{{item.description}}</td>
<td>{{item.quantity}}</td>
</tr>
</tbody>
<tfoot>
<tr class="bg-warning">
<td><font size="6">סך הכל מוצרים במלאי:</font></td>
<td><font size="6">{{total}}</font></td>
<td></td>
</tr>
</tfoot>
</table>
</div>
计算的控制器功能:
$scope.total = 0;
$scope.setTotals = function(item){
// $scope.total = 0;
if (item){
$scope.total += parseInt(item.quantity);
console.log($scope.total);
return $scope.total;
}
}
答案 0 :(得分:0)
好像你想要表条目的所有quantity
的总和。然后你还需要更改HTML代码
<强> HTML 强>:
<tbody>
<tr ng-repeat="item in stockReport" ng-if = "item.category_name == stockReport.selectedOption.category_name"
ng-init="setTotals(stockReport)">
<td>{{item.category_name}}</td>
<td>{{item.stock_id}}</td>
<td>{{item.product_name}}</td>
<td>{{item.description}}</td>
<td>{{item.quantity}}</td>
</tr>
</tbody>
或强>
<select ng-options="......" ng-change="setTotals(stockReport)"></select> //this is better option and remove ng-init
<强> JS 强>:
$scope.setTotals = function(totalItem){
$scope.total = 0;
for(var item in totalItem){
if (totalItem[item] && (totalItem[item].category_name == $scope.stockReport.selectedOption.category_name)){
$scope.total += parseInt(totalItem[item].quantity);
console.log($scope.total);
return $scope.total;
}
}
}
答案 1 :(得分:0)
res
。它在文档中清楚地说明了。
如果您希望每次选择更改时计算总数,请使用ng-change:
ng-init
或者,如果总计算足够快,只需在每次需要时计算它:
<select ng-model="stockReport.selectedOption" ng-change="computeTotal()" ...>
computeTotal函数应如下所示:
<td><font size="6">{{ computeTotal() }}</font></td>
请注意,您不应使用$scope.computeTotal = function() {
var total = 0;
$scope.stockReport.forEach(function(item) {
if (item.category_name == stockReport.selectedOption.category_name) {
total += parseInt(item.quantity)
}
});
$scope.total = total;
// or, if you choose the second option of computing it every time you need it:
// return total;
}
。使用CSS。