angular ng-grid selected Items 在第一次迭代后为空白

angular ng-grid selected Items is blank after first iteration

我正在 ng-grid 上进行批量操作。选择所有行时,第一次选择的项目是正确的,但在第一次批量操作后,选择的项目是空白的。使用下面的代码,它会发送一条通知 "Must select at least 1 row to move." 如果您需要更多详细信息,请告诉我。提前致谢。

vm.init = function() {
    vm.title = "Orphan Entries";
    selectReport("OrphanEntries");
};
/* initialization */
vm.init();

vm.gridOptionsReports = {
            data: 'vm.data',
            multiSelect: true,
            enableColumnResize: true,
            showFooter: true,
            showSelectionCheckbox: true,
            enableRowSelection: true,
            selectWithCheckboxOnly: true,
            selectedItems: [],
            selectionCheckboxColumnWidth: 25,
            enablePaging: true,
            pagingOptions: $scope.pagingOptions
        } 

$scope.applyBulkAction = function(){
    if(vm.selectedAction == 'notset'){
       Notification.error("You need to select a bulk action.");
    }else{
        var rows = vm.gridOptionsReports.selectedItems;
        if(rows.length < 1){
            Notification.error("Must select at least 1 row to move.");
        }else{
           var toggleTo = 0;
           if(vm.selectedAction == 'move'){
                toggleTo = 1;
            }
            var data = "";
            var first =true;
            for(var i = 0; i< rows.length; i++){
                if(rows[i].selectedToBeMoved != toggleTo){
                    if(first){
                        data = '{"ID":"'+rows[i].iD+'"}';
                        first = false;
                    }else{
                        data = data + ',{"ID":"'+rows[i].iD+'"}'
                    }
                }
            }
            if(data == ""){
               Notification.error("The rows that you have selected have already been flagged.")
                }else{
                    return ReportService.flagOrphansToBeMoved(data).then(flagOrphansToBeMovedComplete).catch(flagOrphansToBeMovedFailed);

                    function flagOrphansToBeMovedComplete(response) {
                        if(!response.data.error && response.data.status == 200){
                            Notification.success('Bulk action was successful.  All flagged rows will be moved during the next scan.');
                            //vm.gridOptionsReports.selectedItems = [];
                            vm.init();
                        }else{
                            Notification.error('Error scheduling to move orphan.');
                        }
                        return response;
                    }

                    function flagOrphansToBeMovedFailed(error) {
                        Notification.error('Error scheduling to move orphan.');
                    }
                }
            }
        }
    }

UI

UI代码:

<div class="row smallPaddingBot">
<div class="col-sm-2">
    <select ng-model="vm.selectedAction" class="form-control">
        <option value="notset">Bulk Actions</option>
        <option value="move">Flag Selected To Be Moved</option>
        <option value="dontMove">Flag Selected Not To Moved</option>
    </select>
</div>
<div class="col-sm-1">
    <button type="button" class="btn btn-default" ng-click="applyBulkAction()">Apply</button>
</div>

问题是我每次都在重新创建 ng-grid,所以我迷失了范围。解决方案是将 gridOptionsReports.selectedItems 设置为范围变量,然后引用该范围变量。

发件人:

selectedItems: [],

收件人:

selectedItems: vm.selectedRow,

并且:

var rows = vm.gridOptionsReports.selectedItems;

收件人:

var rows = vm.selectedRows;