UI-Grid - 如何使用selectAll复选框选择当前页面中显示的记录

时间:2016-08-24 12:31:26

标签: javascript angularjs ui-grid

在UI-grid"选择全部"复选框,选中后,选择当前页面以及其他页面中可见的所有记录。

问题 - 我们选择当前页面中显示的行的方式是什么。

plunker http://plnkr.co/edit/gHiER0A2Oaia4HMWChcG?p=preview

尝试了api $scope.gridApi.selection.selectAllVisibleRows();,但它没有选择当前页面中显示的行。如果单击按钮"选择可见行"并转到下一页,那里的记录也被选中。

api的其他详细信息selectAllVisibleRows

在ui-grid函数中选中selectAllVisibleRows。 row.visible会为true返回all the rows across all pages


selectAllVisibleRows: function (evt) {
    if (grid.options.multiSelect === false) {
        return;
     }

    var changedRows = [];
        grid.rows.forEach(function (row) {
        if (row.visible) {
           if (!row.isSelected && row.enableSelection !== false){
              row.setSelected(true);
              service.decideRaiseSelectionEvent( grid, row, changedRows, evt );
           }
        } else {
          if (row.isSelected){
             row.setSelected(false);
             service.decideRaiseSelectionEvent( grid, row, changedRows, evt );
          }
        }});
    service.decideRaiseSelectionBatchEvent( grid, changedRows, evt );
    grid.selection.selectAll = true;
},

3 个答案:

答案 0 :(得分:2)

尝试使用 for循环这里operations on arrays

答案 1 :(得分:0)

要仅选择当前网格页面中的行,必须在注册其API时为ui-grid v4.4.9 rowSelectionChangedBatch事件添加处理程序。方法。正如您所指出的,它会将未过滤的所有行视为可见。它不排除未在当前网格页面上呈现的行。因此,我们被迫将事情掌握在自己手中:

        onRegisterApi: function (gridApi) {

            gridApi.selection.on.rowSelectionChangedBatch(null, function (rows) {
                var grid = this.grid;
                // you'll need ui-grid v4.4.9 or better 
                // otherwise you'll need to invert getSelectAllState()
                // as explained at https://github.com/angular-ui/ui-grid/issues/5411
                var isAllSelected = grid.api.selection.getSelectAllState();
                // when select all is checked, the default behavior is to select all grid rows on all pages
                // so we have to deselect them all first before we select just the ones on the visible page
                grid.api.selection.clearSelectedRows(null);
                if (isAllSelected) {
                    // select only the rows displayed in the current grid page
                    var startIndex = (grid.options.paginationCurrentPage - 1) * grid.options.paginationPageSize;
                    var endIndex = startIndex + grid.options.paginationPageSize;
                    for (let i = startIndex; i < endIndex; i++) {
                        let row = grid.rows[i];
                        row.isSelected = true;
                    }
                }
            });
        },

答案 2 :(得分:0)

onRegisterApi: function(gridApi) {
    gridApi.selection.on.rowSelectionChangedBatch($scope, function() {

        // check for ui-grid version to revert opposite value
        var isAllSelected = gridApi.selection.getSelectAllState();

        if (isAllSelected) { 

            // unselect all rows as select all button selects all rows by default
            gridApi.selection.clearSelectedRows();

            /* Will fetch only grid rows visible on page. It will fetch the correct 
               rows even if you have applied sorting, pagination and filters. */
            var gridRows = this.grid.getVisibleRows();


            // function to set Selected to only visible rows
            angular.forEach(gridRows, function(gridRow) { 
                gridRow.setSelected(true);
            });
        }
        $scope.selectedRows = gridApi.selection.getSelectedRows();
   });
}