Skip to content

Commit

Permalink
#794 - Add an event that exposes the internal selected row ids.
Browse files Browse the repository at this point in the history
Make DataView. syncGridSelection() return an event that notifies when
an internal list of selected row ids changes.
  • Loading branch information
mleibman committed Nov 18, 2013
1 parent 35e6449 commit bbe9bfb
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions slick.dataview.js
Expand Up @@ -852,19 +852,36 @@
* @param preserveHiddenOnSelectionChange {Boolean} Whether to keep selected items
* that are currently out of the view (see preserveHidden) as selected when selection
* changes.
* @return {Slick.Event} An event that notifies when an internal list of selected row ids
* changes. This is useful since, in combination with the above two options, it allows
* access to the full list selected row ids, and not just the ones visible to the grid.
* @method syncGridSelection
*/
function syncGridSelection(grid, preserveHidden, preserveHiddenOnSelectionChange) {
var self = this;
var inHandler;
var selectedRowIds = self.mapRowsToIds(grid.getSelectedRows());
var onSelectedRowIdsChanged = new Slick.Event();

function setSelectedRowIds(rowIds) {
if (selectedRowIds.join(",") == rowIds.join(",")) {
return;
}

selectedRowIds = rowIds;

onSelectedRowIdsChanged.notify({
"grid": grid,
"ids": selectedRowIds
}, new Slick.EventData(), self);
}

function update() {
if (selectedRowIds.length > 0) {
inHandler = true;
var selectedRows = self.mapIdsToRows(selectedRowIds);
if (!preserveHidden) {
selectedRowIds = self.mapRowsToIds(selectedRows);
setSelectedRowIds(self.mapRowsToIds(selectedRows));
}
grid.setSelectedRows(selectedRows);
inHandler = false;
Expand All @@ -875,18 +892,20 @@
if (inHandler) { return; }
var newSelectedRowIds = self.mapRowsToIds(grid.getSelectedRows());
if (!preserveHiddenOnSelectionChange || !grid.getOptions().multiSelect) {
selectedRowIds = newSelectedRowIds;
setSelectedRowIds(newSelectedRowIds);
} else {
// keep the ones that are hidden
selectedRowIds = $.grep(selectedRowIds, function(id) { return self.getRowById(id) === undefined; });
var existing = $.grep(selectedRowIds, function(id) { return self.getRowById(id) === undefined; });
// add the newly selected ones
selectedRowIds = selectedRowIds.concat(newSelectedRowIds);
setSelectedRowIds(existing.concat(newSelectedRowIds));
}
});

this.onRowsChanged.subscribe(update);

this.onRowCountChanged.subscribe(update);

return onSelectedRowIdsChanged;
}

function syncGridCellCssStyles(grid, key) {
Expand Down

0 comments on commit bbe9bfb

Please sign in to comment.