Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 35e6449

Browse files
committed
Fix #794 - Add an option to keep out-of-the-view items selected when selection changes (preserveHiddenOnSelectionChange)
1 parent 3377e2d commit 35e6449

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

slick.dataview.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@
304304
function mapIdsToRows(idArray) {
305305
var rows = [];
306306
ensureRowsByIdCache();
307-
for (var i = 0; i < idArray.length; i++) {
307+
for (var i = 0, l = idArray.length; i < l; i++) {
308308
var row = rowsById[idArray[i]];
309309
if (row != null) {
310310
rows[rows.length] = row;
@@ -315,7 +315,7 @@
315315

316316
function mapRowsToIds(rowArray) {
317317
var ids = [];
318-
for (var i = 0; i < rowArray.length; i++) {
318+
for (var i = 0, l = rowArray.length; i < l; i++) {
319319
if (rowArray[i] < rows.length) {
320320
ids[ids.length] = rows[rowArray[i]][idProperty];
321321
}
@@ -838,10 +838,26 @@
838838
}
839839
}
840840

841-
function syncGridSelection(grid, preserveHidden) {
841+
/***
842+
* Wires the grid and the DataView together to keep row selection tied to item ids.
843+
* This is useful since, without it, the grid only knows about rows, so if the items
844+
* move around, the same rows stay selected instead of the selection moving along
845+
* with the items.
846+
*
847+
* NOTE: This doesn't work with cell selection model.
848+
*
849+
* @param grid {Slick.Grid} The grid to sync selection with.
850+
* @param preserveHidden {Boolean} Whether to keep selected items that go out of the
851+
* view due to them getting filtered out.
852+
* @param preserveHiddenOnSelectionChange {Boolean} Whether to keep selected items
853+
* that are currently out of the view (see preserveHidden) as selected when selection
854+
* changes.
855+
* @method syncGridSelection
856+
*/
857+
function syncGridSelection(grid, preserveHidden, preserveHiddenOnSelectionChange) {
842858
var self = this;
843-
var selectedRowIds = self.mapRowsToIds(grid.getSelectedRows());;
844859
var inHandler;
860+
var selectedRowIds = self.mapRowsToIds(grid.getSelectedRows());
845861

846862
function update() {
847863
if (selectedRowIds.length > 0) {
@@ -857,7 +873,15 @@
857873

858874
grid.onSelectedRowsChanged.subscribe(function(e, args) {
859875
if (inHandler) { return; }
860-
selectedRowIds = self.mapRowsToIds(grid.getSelectedRows());
876+
var newSelectedRowIds = self.mapRowsToIds(grid.getSelectedRows());
877+
if (!preserveHiddenOnSelectionChange || !grid.getOptions().multiSelect) {
878+
selectedRowIds = newSelectedRowIds;
879+
} else {
880+
// keep the ones that are hidden
881+
selectedRowIds = $.grep(selectedRowIds, function(id) { return self.getRowById(id) === undefined; });
882+
// add the newly selected ones
883+
selectedRowIds = selectedRowIds.concat(newSelectedRowIds);
884+
}
861885
});
862886

863887
this.onRowsChanged.subscribe(update);

0 commit comments

Comments
 (0)