[英]Remove elements from array based on filter in angularjs
如何根据过滤器从数组中删除元素,当'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);
}
});
}
<table data-ng-repeat="st in Absorb | filter:{isSelected: '!false', ProjectedQuantityOnHand: '!0'} track by $index" data-ng-cloak>
这将仅显示那些isSelected与false不同且ProjectedQuantityOnHand不同于0的项目。不需要外部函数。
UPDATE
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;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.