1

在 UI 网格中的“全选”复选框中,选中时,会选择当前页面以及其他页面中可见的所有记录。

问题- 我们可以通过什么方式选择仅显示在当前页面中的行。

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;
},
4

3 回答 3

2

尝试使用for 循环这是plunker 链接

于 2016-08-25T11:58:02.080 回答
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();
   });
}
于 2019-09-19T21:22:03.270 回答
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;
                    }
                }
            });
        },
于 2018-05-15T20:05:17.003 回答