我正在尝试构建一个简单的页面来分组记录,然后添加一个按钮来消除一些记录。
问题在于,如果删除的记录是第一个,则将删除所有分组数据。
我认为问题是当我拆分分组数组时:
Plunkr http://plnkr.co/edit/DiO0tiPWeyRDGCoeHpdW?p=preview
$scope.deleteItem = function(item,row){
var index=$scope.reports.indexOf(item)
$scope.reports.splice(index,1);
$scope.noOfPages = Math.ceil($scope.reports.length / $scope.pageSize);
$scope.pages.then(function(result){
result.splice(index,1);
$scope.noOfPages = Math.ceil(result.length / $scope.pageSize);
$scope.currentPage = 1;
console.log(result)
})
};
答案 0 :(得分:0)
你主要做错2件事:
可能的(工作)修复方法是:
$scope.deleteItem = function(item,row){
var lastInRow = true;
var index=$scope.reports.indexOf(item);
$scope.reports.splice(index,1);
//check if any more items match that name (or in the same row)
for (var i=0; i<$scope.reports.length; ++i) {
if ($scope.reports[i].name == item.name) {
lastInRow = false;
break;
}
}
$scope.noOfPages = Math.ceil($scope.reports.length / $scope.pageSize);
$scope.pages.then(function(result){
//get the relevant index for the result array
var resultIndex = result.indexOf(item.name);
//if it matches and it isn't the last one - erase that row
if (result && result[index] == item.name && lastInRow) {
result.splice(resultIndex,1);
}
$scope.noOfPages = Math.ceil(result.length / $scope.pageSize);
$scope.currentPage = 1;
})
};