如何根据过滤器从数组中删除元素,当'quantity'为0且'isSelected'为false时,仅删除那些索引?任何建议。这是我的代码:
<table data-ng-repeat="st in Absorb track by $index" data-ng-cloak data-ng-init="remove(st)">
<tr>
<td>
<input type="radio" name="groupName" data-ng-model ="st.isSelected" data-ng-checked="(st.isSelected == true)"/>
</td>
<td>
<span>{{st.ProjectedQuantityOnHand}}</span>
</td>
<td style="display:none" data-ng-if="st.ProjectedQuantityOnHand == 0">
<input type="number" data-ng-model="st.ProjectedQuantityOnHand" style="display:none">
</td>
</tr>
</table>
JS代码:
$scope.Absorb = [
{"Name":"apple", ProjectedQuantityOnHand:"0", isSelected:true},
{"Name":"mango", ProjectedQuantityOnHand:"0", isSelected:false}
{"Name":"ball", ProjectedQuantityOnHand:"1", isSelected:false}
{"Name":"football", ProjectedQuantityOnHand:"1", isSelected:false}
]
$scope.remove = function (item) {
var availableqauantity = 0
debugger
angular.forEach($scope.StockList, function (i) {
var FilteredProduct1 = $filter('filter')($scope.StockList, { isSelected: false, ProjectedQuantityOnHand: 0 });
if (FilteredProduct1.length > 0) {
availableqauantity = FilteredProduct1[0].ProjectedQuantityOnHand;
if (availableqauantity == 0)
$scope.StockList.splice(i,1);
}
});
}
答案 0 :(得分:2)
<table data-ng-repeat="st in Absorb | filter:{isSelected: '!false', ProjectedQuantityOnHand: '!0'} track by $index" data-ng-cloak>
这将只显示那些isSelected不同于false且ProjectedQuantityOnHand不同于0的项目。不需要外部函数。
<强>更新强>
https://plnkr.co/edit/gPz5pKSIOxZ6odSU3TfF?p=preview
检查此示例。我在这里制作了一个自定义过滤器,只有当ProjectedQuantityOnHand为0且isSelected为false时才隐藏项目。
app.filter('customFilter', function() {
return function(values) {
var filtderResult = [];
angular.forEach(values, function(value) {
if (value.isSelected || value.ProjectedQuantityOnHand !== 0) {
filtderResult.push(value);
}
});
return filtderResult;
}
});