diff options
Diffstat (limited to 'slick.dataview.js')
-rw-r--r-- | slick.dataview.js | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/slick.dataview.js b/slick.dataview.js index 2dabd87..8103714 100644 --- a/slick.dataview.js +++ b/slick.dataview.js @@ -304,7 +304,7 @@ function mapIdsToRows(idArray) { var rows = []; ensureRowsByIdCache(); - for (var i = 0; i < idArray.length; i++) { + for (var i = 0, l = idArray.length; i < l; i++) { var row = rowsById[idArray[i]]; if (row != null) { rows[rows.length] = row; @@ -315,7 +315,7 @@ function mapRowsToIds(rowArray) { var ids = []; - for (var i = 0; i < rowArray.length; i++) { + for (var i = 0, l = rowArray.length; i < l; i++) { if (rowArray[i] < rows.length) { ids[ids.length] = rows[rowArray[i]][idProperty]; } @@ -838,10 +838,26 @@ } } - function syncGridSelection(grid, preserveHidden) { + /*** + * Wires the grid and the DataView together to keep row selection tied to item ids. + * This is useful since, without it, the grid only knows about rows, so if the items + * move around, the same rows stay selected instead of the selection moving along + * with the items. + * + * NOTE: This doesn't work with cell selection model. + * + * @param grid {Slick.Grid} The grid to sync selection with. + * @param preserveHidden {Boolean} Whether to keep selected items that go out of the + * view due to them getting filtered out. + * @param preserveHiddenOnSelectionChange {Boolean} Whether to keep selected items + * that are currently out of the view (see preserveHidden) as selected when selection + * changes. + * @method syncGridSelection + */ + function syncGridSelection(grid, preserveHidden, preserveHiddenOnSelectionChange) { var self = this; - var selectedRowIds = self.mapRowsToIds(grid.getSelectedRows());; var inHandler; + var selectedRowIds = self.mapRowsToIds(grid.getSelectedRows()); function update() { if (selectedRowIds.length > 0) { @@ -857,7 +873,15 @@ grid.onSelectedRowsChanged.subscribe(function(e, args) { if (inHandler) { return; } - selectedRowIds = self.mapRowsToIds(grid.getSelectedRows()); + var newSelectedRowIds = self.mapRowsToIds(grid.getSelectedRows()); + if (!preserveHiddenOnSelectionChange || !grid.getOptions().multiSelect) { + selectedRowIds = newSelectedRowIds; + } else { + // keep the ones that are hidden + selectedRowIds = $.grep(selectedRowIds, function(id) { return self.getRowById(id) === undefined; }); + // add the newly selected ones + selectedRowIds = selectedRowIds.concat(newSelectedRowIds); + } }); this.onRowsChanged.subscribe(update); |