|
304 | 304 | function mapIdsToRows(idArray) {
|
305 | 305 | var rows = [];
|
306 | 306 | ensureRowsByIdCache();
|
307 |
| - for (var i = 0; i < idArray.length; i++) { |
| 307 | + for (var i = 0, l = idArray.length; i < l; i++) { |
308 | 308 | var row = rowsById[idArray[i]];
|
309 | 309 | if (row != null) {
|
310 | 310 | rows[rows.length] = row;
|
|
315 | 315 |
|
316 | 316 | function mapRowsToIds(rowArray) {
|
317 | 317 | var ids = [];
|
318 |
| - for (var i = 0; i < rowArray.length; i++) { |
| 318 | + for (var i = 0, l = rowArray.length; i < l; i++) { |
319 | 319 | if (rowArray[i] < rows.length) {
|
320 | 320 | ids[ids.length] = rows[rowArray[i]][idProperty];
|
321 | 321 | }
|
|
838 | 838 | }
|
839 | 839 | }
|
840 | 840 |
|
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) { |
842 | 858 | var self = this;
|
843 |
| - var selectedRowIds = self.mapRowsToIds(grid.getSelectedRows());; |
844 | 859 | var inHandler;
|
| 860 | + var selectedRowIds = self.mapRowsToIds(grid.getSelectedRows()); |
845 | 861 |
|
846 | 862 | function update() {
|
847 | 863 | if (selectedRowIds.length > 0) {
|
|
857 | 873 |
|
858 | 874 | grid.onSelectedRowsChanged.subscribe(function(e, args) {
|
859 | 875 | 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 | + } |
861 | 885 | });
|
862 | 886 |
|
863 | 887 | this.onRowsChanged.subscribe(update);
|
|
0 commit comments