基本上它的kendo网格默认数据的dataSource是selVal,如果selval的任何键与dtoVal匹配我想删除所有内容 从selVa,是否可能使用下面的代码实现它不从数组中删除selVal元素。有什么想法吗?
ctrl.js
$scope.handleControlOwnerCancel = function(){
this.ctrlOwnerModal.close();
var dtoVal = $scope.controlDTO.controlOwners;
var selVal = $scope.selectedOwners;
for (var i = 0; i < dtoVal.length; i++) {
for (var j=0; j<selVal.length; j++) {
if (dtoVal[i].workerKey !== selVal[j].workerKey) {
$scope.selectedOwners = $scope.controlDTO.controlOwners;
controlOwnersDataSource();
}
}
}
};
答案 0 :(得分:0)
最好使用angular.forEach
而不是for
循环。你也说过
如果selval中的任何键与dtoVal匹配,我想删除 来自selVa的一切
但是在循环内部,您正在使用!==
进行检查。我不知道哪一个是你的要求。但你可以尝试这样做:
$scope.handleControlOwnerCancel = function(){
this.ctrlOwnerModal.close();
//var dtoVal = $scope.controlDTO.controlOwners;
//var selVal = $scope.selectedOwners;
var match = 0;
angular.forEach($scope.selectedOwners, function(val, key){
angular.forEach($scope.controlDTO.controlOwners, function(val2, key2){
if(val.workerKey==val2.workerKey){
match++;
}
})
})
if(match>0){
$scope.selectedOwners = $scope.controlDTO.controlOwners;
controlOwnersDataSource();
}
};
答案 1 :(得分:0)
试试这个
$scope.handleControlOwnerCancel = function(){
this.ctrlOwnerModal.close();
var dtoVal = $scope.controlDTO.controlOwners;
var selVal = $scope.selectedOwners;
var selValCopy = angular.copy(selVal);
for (var i=dtoVal.length-1; i>=0; i--) {
for (var j=selVal.length-1; j>=0; j--) {
if (dtoVal[i].workerKey === selVal[j].workerKey) {
selValCopy.splice(j, 1);
} else {
$scope.selectedOwners = $scope.controlDTO.controlOwners;
controlOwnersDataSource();
}
}
}
selVal = selValCopy;
};