Skip to content

SelectionPlugin

Selection Plugin for tbw-grid

Adds cell, row, and range selection capabilities to the grid with full keyboard support. Whether you need simple cell highlighting or complex multi-range selections, this plugin has you covered.

import { SelectionPlugin } from '@toolbox-web/grid/plugins/selection';

Configure the plugin with one of three modes via SelectionConfig:

  • 'cell' - Single cell selection (default). Click cells to select individually.
  • 'row' - Full row selection. Click anywhere in a row to select the entire row.
  • 'range' - Rectangular selection. Click and drag or Shift+Click to select ranges.
ShortcutAction
Arrow KeysMove selection
Shift + ArrowExtend selection (range mode)
Ctrl/Cmd + ClickToggle selection (multi-select)
Shift + ClickExtend to clicked cell/row
Ctrl/Cmd + ASelect all (range mode)
EscapeClear selection

> Note: When multiSelect: false, Ctrl/Shift modifiers are ignored — > clicks always select a single item.

PropertyDescription
--tbw-focus-backgroundFocused row background
--tbw-range-selection-bgRange selection fill
--tbw-range-border-colorRange selection border
grid.gridConfig = {
columns: [...],
plugins: [new SelectionPlugin({ mode: 'row' })],
};
grid.gridConfig = {
plugins: [new SelectionPlugin({ mode: 'range' })],
};
grid.on('selection-change', ({ mode, ranges }) => {
console.log(`Selected ${ranges.length} ranges in ${mode} mode`);
});
const plugin = grid.getPluginByName('selection');
// Get current selection
const selection = plugin.getSelection();
console.log(selection.ranges);
// Set selection programmatically
plugin.setRanges([{ from: { row: 0, col: 0 }, to: { row: 5, col: 3 } }]);
// Clear all selection
plugin.clearSelection();

Extends BaseGridPlugin

Inherited methods like attach(), detach(), afterRender(), etc. are documented in the base class.

Get the current selection as a unified result. Works for all selection modes and always returns ranges.

getSelection(): SelectionResult
const selection = plugin.getSelection();
if (selection.ranges.length > 0) {
const { from, to } = selection.ranges[0];
// For cell mode: from === to (single cell)
// For row mode: from.col = 0, to.col = lastCol (full row)
// For range mode: rectangular selection
}

Get all selected cells across all ranges.

getSelectedCells(): object[]

Check if a specific cell is in range selection.

isCellSelected(row: number, col: number): boolean
NameTypeDescription
rownumber
colnumber

Select all selectable rows (row mode) or all cells (range mode).

In row mode, selects every row where isSelectable returns true (or all rows if no callback). In range mode, creates a single range spanning all rows and columns. Has no effect in cell mode.

selectAll(): void
const plugin = grid.getPluginByName('selection');
plugin.selectAll(); // Selects everything in current mode

Select specific rows by index (row mode only). Replaces the current selection with the provided row indices. Indices that are out of bounds or fail the isSelectable check are ignored.

selectRows(indices: number[]): void
NameTypeDescription
indicesnumber[]Array of row indices to select
const plugin = grid.getPluginByName('selection');
plugin.selectRows([0, 2, 4]); // Select rows 0, 2, and 4

Get the indices of all selected rows (convenience for row mode). Returns indices sorted in ascending order.

getSelectedRowIndices(): number[]
const plugin = grid.getPluginByName('selection');
const rows = plugin.getSelectedRowIndices(); // [0, 2, 4]

Get the actual row objects for the current selection.

Works across all selection modes:

  • Row mode: Returns the row objects for all selected rows.
  • Cell mode: Returns the single row containing the selected cell, or [].
  • Range mode: Returns the unique row objects that intersect any selected range.

Row objects are resolved from the grid’s processed (sorted/filtered) row array, so they always reflect the current visual order.

getSelectedRows(): T[]
const plugin = grid.getPluginByName('selection');
const selected = plugin.getSelectedRows(); // [{ id: 1, name: 'Alice' }, ...]

Clear all selection.

clearSelection(): void

Set selected ranges programmatically.

setRanges(ranges: CellRange[]): void
NameTypeDescription
rangesCellRange[]

AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt