Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SlickGrid: How to keep selected rows in paginated grids?

I'm using a paginated version of SlickGrid in conjunction with the CheckboxSelection-Plugin. Unfortunately, the row selection gets lost when one leaves the current grid page and isn't restored when that page is accessed again. This behaviour can also be reproduced with SlickGrid example #4. (click the little light-bulb-icon in the grid footer to turn on pagination)

I've informed SlickGrid's author about this, but he doesn't see this behaviour as a bug, as SlickGrid is more a grid framework than a just-add-data-solution.

Does anyone have an idea on how to implement reliable selection persistence in SlickGrid, that works for both paginated and non-paginated grids?

like image 558
fbuchinger Avatar asked Dec 11 '25 10:12

fbuchinger


1 Answers

Finally I had some success by adapting the grid.onSelectedRowsChanged callback in Example 4. The key difference is that I don't reset the global selectedRowIds array each time the selection changes, since grid.getSelectedRows() only gives me the selected row indices on the current page. Instead I first add all newly selected rows to the selectedRowIds array and then iterate over it to check if one of its current-page-entries has been deselected.

grid.onSelectedRowsChanged.subscribe(function (e) {
    var selRows = grid.getSelectedRows();
    // add all newly selected rows to the selection
    for (var i = 0, l = selRows.length; i < l; i++) {
        var item = dataView.getItem(selRows[i]);
        if (item) {
            var rowId = item.id;
            if (selectedRowIds.indexOf(rowId) < 0) {
                selectedRowIds.push(rowId);
            }
        }
    }

    //check if a previously selected row has been deselected
    for (var i = 0, l = selectedRowIds.length; i < l; i++) {
        var row = dataView.getRowById(selectedRowIds[i]); // see if the selected row is   available on the current page
        if (row && selRows.indexOf(row) < 0) {
            selectedRowIds.splice(i, 1); //remove row from array
        }
    }
   });
like image 161
fbuchinger Avatar answered Dec 14 '25 00:12

fbuchinger