-
-
Notifications
You must be signed in to change notification settings - Fork 120
Row Selection
- Single Row Selection
- Multiple Row Selections
- Change Dynamically Single/Multiple Selections
- Mixing Single & Multiple Row Selections
- Disable Custom Rows Selections via
selectableOverride
- Disable External Button when having Empty Selection
- Change Row Selections
- Troubleshooting
For row selection, you can simply play with couple of grid options (see below) and subscribe to onSelectedRowsChanged
(a SlickGrid Event that is, it's not an Observable). However please note that onSelectedRowsChanged
is a function available on the Grid
object and you will need bind to (gridChanged)
to get the object when grid is ready. There are 2 types of row selection(s) which you can do.
Note: enableCheckboxSelector
and enableExcelCopyBuffer
do not work well together, this is because they both share the same Row.SelectionModel
and one cancels the other. It is recommended to not use enableExcelCopyBuffer
in that case.
For a single row selection, you need to have enableCellNavigation: true
, enableRowSelection: true
and multiSelect: false
and as described earlier, subscribe to onSelectedRowsChanged
(for that you need to bind to (gridChanged)
).
Note: if you want to change from Multiple Selections to Single Selection (and vice-versa), you could use the grid options enableCellNavigation
flag, however this is not possible when using Inline Editors since this flag is required. However, there is no other known ways of toggling dynamically.
<angular-slickgrid gridId="grid4"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onSelectedRowsChanged)="onSelectedRowsChanged($event.detail.eventData, $event.detail.args)">
</angular-slickgrid>
this.gridOptions = {
enableAutoResize: true,
enableCellNavigation: true,
enableCheckboxSelector: true,
enableRowSelection: true,
multiSelect: false,
}
onSelectedRowsChanged(e, args) {
if (Array.isArray(args.rows)) {
this.selectedObjects = args.rows.map(idx => {
const item = grid.getDataItem(idx);
return item.title || '';
});
}
}
As for multiple row selections, you need to provide an extra grid option of rowSelectionOptions
which is an object and within it, you need to disable the selectActiveRow
flag. The other configurations are the same as a Single Selection, which is to enable enableCheckboxSelector
and enableRowSelection
. Then as describe earlier, you will subscribe to onSelectedRowsChanged
(for that you need to bind to (gridChanged)
).
<angular-slickgrid gridId="grid4"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onSelectedRowsChanged)="onSelectedRowsChanged($event.detail.eventData, $event.detail.args)">
</angular-slickgrid>
export class Example1 implements OnInit {
ngOnInit() {
this.gridOptions = {
enableAutoResize: true,
enableCellNavigation: true,
enableCheckboxSelector: true,
enableRowSelection: true,
rowSelectionOptions: {
// True (Single Selection), False (Multiple Selections)
selectActiveRow: false
},
}
}
onSelectedRowsChanged(e, args) {
if (Array.isArray(args.rows)) {
// user clicked on the 1st column, multiple checkbox selection
console.log('multiple row checkbox selected', event, args);
}
}
If you want to change from Multiple Selections to Single Selection (and vice-versa), you could toggle the grid options enableCellNavigation
flag (False
when you want Single Selection), however this is not possible when using Inline Editors since this flag is required. Note that there is currently no other ways of toggling dynamically without re-creating the grid.
SlickGrid is so powerful and customizable, you could if you wish mix the multiple row selections (cell column 1) and single row selection (any other cell click). For that though, you will need to use 2 SlickGrid Events (onClick
and onSelectedRowsChanged
). For example we can do it this way:
<angular-slickgrid gridId="grid4"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onSelectedRowsChanged)="onSelectedRowsChanged($event.detail.eventData, $event.detail.args)"
(onClick)="onCellClicked($event.detail.eventData, $event.detail.args)">
</angular-slickgrid>
export class Example1 implements OnInit {
onSelectedRowsChanged(e, args) {
// user clicked on the 1st column, multiple checkbox selection
console.log('multiple row checkbox selected', event, args);
}
onCellClicked(e, args) {
// when clicking on any cell, we will make it the new selected row
// however, we don't want to interfere with multiple row selection checkbox which is on 1st column cell
if (args.cell !== 0) {
grid.setSelectedRows([args.row]);
}
}
}
You can use selectableOverride
to provide custom logic to disable certain rows selections, for example the code below will remove the row selection on every second row.
export class Example1 implements OnInit {
prepareGrid() {
this.gridOptions = {
enableRowSelection: true,
enableCheckboxSelector: true,
checkboxSelector: {
// you can override the logic for showing (or not) the expand icon
// for example, display the expand icon only on every 2nd row
selectableOverride: (row: number, dataContext: any, grid: any) => (dataContext.id % 2 === 1)
},
multiSelect: false,
rowSelectionOptions: {
// True (Single Selection), False (Multiple Selections)
selectActiveRow: true,
},
};
}
}
When having an external button that you want to work only when there's row selection, there are 2 ways of doing this.
- use the
onSelectedRowsChanged
event (via your View in HTML or via ViewModel)
<button disabled.bind="isMyButtonDisabled">My Button</button>
<angular-slickgrid gridId="grid2"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onSelectedRowsChanged)="handleOnSelectedRowsChanged($event.detail.args)">
</angular-slickgrid>
isMyButtonDisabled = false;
handleOnSelectedRowsChanged(args) {
this.isMyButtonDisabled = args?.rows?.length === 0;
}
- use the
onGridStateChanged
event (see Grid State & Presets Wiki)
<button disabled.bind="isMyButtonDisabled">My Button</button>
<angular-slickgrid gridId="grid2"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onGridStateServiceChanged)="handleOngridStateChanged($event.detail.args)">
</angular-slickgrid>
isMyButtonDisabled = false;
handleOngridStateChanged(gridState) {
if (Array.isArray(gridState?.rowSelection.dataContextIds)) {
this.isMassSelectionDisabled = gridState.rowSelection.dataContextIds.length === 0;
}
}
You can change which row(s) are selected by using the built-in SlickGrid method setSelectedRows(rowIndexes)
(passing an empty array will clear all selection), however please note that it requires an array of row indexes as you see them in the UI and it won't work that great with Pagination (if that is what you are looking for then take a look at this Stack Overflow Q&A)
<angular-slickgrid gridId="grid2"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onAngularGridCreated)="angularGridReady($event.detail)">
</angular-slickgrid>
export class Example1 {
angularGrid: AngularGridInstance;
angularGridReady(angularGrid: AngularGridInstance) {
this.angularGrid = angularGrid;
this.gridObj = angularGrid.slickGrid;
}
changeRowSelections() {
this.angularGrid.slickGrid.setSelectedRows(rowIndexes);
// OR providing an empty array will clear the row selection
// this.angularGrid.slickGrid.setSelectedRows([]);
}
}
The reason is because the Row Selection (checkbox) plugin is a special column and Angular-Slickgrid is adding an extra column dynamically for the Row Selection checkbox and that is not reflected in your local copy of columnDefinitions
. To address this issue, you need to get the Angular-Slickgrid internal copy of all columns (including the extra columns), you can get it via getAllColumnDefinitions()
from the Grid Service and then you can use to that array and that will work.
<angular-slickgrid gridId="grid17"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onAngularGridCreated)="angularGridReady($event.detail)">
</angular-slickgrid>
angularGridReady(angularGrid: AngularGridInstance) {
this.angularGrid = angularGrid;
}
addNewColumn() {
const newColumn = { /*...*/ };
const allColumns = this.angularGrid.gridService.getAllColumnDefinitions();
allColumns.push(newColumn);
this.columnDefinitions = allColumns.slice(); // or use spread operator [...cols]
// you could also use SlickGrid setColumns() method
// this.angularGrid.slickGrid.setColumns(cols);
}
Contents
- Angular-Slickgrid Wiki
- Installation
- Styling
- Interfaces/Models
- Testing Patterns
- Column Functionalities
- Global Grid Options
- Localization
- Events
- Grid Functionalities
- Auto-Resize / Resizer Service
- Resize by Cell Content
- Composite Editor
- Context Menu
- Custom Tooltip
- Add/Delete/Update or Highlight item
- Dynamically Change Row CSS Classes
- Excel Copy Buffer
- Export to Excel
- Export to File (CSV/Txt)
- Grid State & Presets
- Grouping & Aggregators
- Row Detail
- SlickGrid Controls
- SlickGrid Plugins
- Pinning (frozen) of Columns/Rows
- Tree Data Grid
- SlickGrid & DataView objects
- Addons (controls/plugins)
- Backend Services