Selection
Configure cell, row, column, and range selection with single or multiple modes.
The SelectionManager provides comprehensive selection capabilities for cells, rows, columns, and ranges with support for both single and multiple selection modes.
Selection Modes
ZenGrid supports two selection modes:
- single: Only one cell, row, column, or range can be selected at a time
- multiple: Multiple selections can be active simultaneously
Selection Types
Configure the type of selection allowed in your grid:
- cell: Select individual cells
- row: Select entire rows
- column: Select entire columns
- range: Select rectangular ranges of cells
Configuration
const grid = new ZenGrid(container, { enableSelection: true, enableMultiSelection: true, selectionType: 'range' // 'cell' | 'row' | 'column' | 'range'}); 💡 Tip
Use range selection for spreadsheet-like behavior, or row selection for table-like interactions.
API Methods
Selecting Cells
// Select a single cellgrid.selectCell(row, col);
// Add to existing selection (additive mode)grid.selectCell(row, col, true);Selecting Rows
// Select a range of rowsgrid.selectRows(startRow, endRow);
// Add rows to existing selectiongrid.selectRows(startRow, endRow, true);Selecting Columns
// Select a range of columnsgrid.selectColumns(startCol, endCol);
// Add columns to existing selectiongrid.selectColumns(startCol, endCol, true);Selecting Ranges
// Select a rectangular rangegrid.selectRange(startRow, startCol, endRow, endCol);
// Add range to existing selectiongrid.selectRange(startRow, startCol, endRow, endCol, true);Querying Selection State
// Check if a cell is selectedconst isSelected = grid.isSelected(row, col);
// Check if a row is selectedconst rowSelected = grid.isRowSelected(row);
// Check if a column is selectedconst colSelected = grid.isColumnSelected(col);
// Get all selected rangesconst ranges = grid.getSelectedRanges();// Returns: [{ startRow, startCol, endRow, endCol }, ...]
// Get selected rowsconst rows = grid.getSelectedRows();
// Get selected columnsconst columns = grid.getSelectedColumns();
// Check if any selection existsconst hasSelection = grid.hasSelection();
// Get selection countconst count = grid.selectionCount;Managing Selection
// Clear all selectionsgrid.clearSelection();
// Change selection mode dynamicallygrid.setMode('single'); // or 'multiple' ℹ Info
When switching from multiple to single mode, only the most recent selection is retained.
Events
Listen to selection changes with these events:
grid.on('selection:change', (event) => { console.log('Selection changed:', event.ranges);});
grid.on('selection:start', (event) => { console.log('Selection started at:', event.cell);});
grid.on('selection:end', (event) => { console.log('Selection ended at:', event.cell);});Keyboard Navigation
💡 Tip
Selection supports standard keyboard shortcuts: arrow keys for navigation, Shift+arrows for range extension, and Ctrl+A for select all.
Example: Multi-Range Selection
const grid = new ZenGrid(container, { enableSelection: true, enableMultiSelection: true, selectionType: 'range'});
// Select first rangegrid.selectRange(0, 0, 2, 2);
// Add second range (additive)grid.selectRange(5, 5, 7, 7, true);
// Get all selectionsconst ranges = grid.getSelectedRanges();console.log(ranges.length); // 2