第一次迭代后,角度ng-grid所选项目为空白

时间:2016-07-29 14:42:51

标签: javascript angularjs ng-grid selecteditem

我正在研究ng-grid上的批量操作。选中所有行时,所选项目首次正确,但在第一次批量操作后,所选项目为空白。使用下面的代码,它会发送一个“必须至少选择一行才能移动”的通知。如果您需要更多详细信息,请与我们联系。提前致谢。

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

enter image description here

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>

    

1 个答案:

答案 0 :(得分:0)

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

自:

c(i)

要:

c

selectedItems: [],

要:

selectedItems: vm.selectedRow,