Row Selection
Enable rowSelection to select single or multiple rows using checkboxes, row clicks, or keyboard Space. Row selection requires a stable row identity strategy (getRowId, rowKey, generateRowId, or rowSource.getRowId).
import { Grid, type ColumnDef } from 'next-grid-js';import { createOrders, type OrderRow } from './data/orders';import { syncGridTheme } from './theme';import type { DemoMountContext } from './types';import { addButton } from './ui';
export function mount({ stage, controls, meta,}: DemoMountContext): () => void { const columnDefs: readonly ColumnDef<OrderRow>[] = [ { key: 'id', name: 'ID', width: 96, isSortable: true, }, { key: 'customer', name: 'Customer', width: 175, flex: 1, isSortable: true, }, { key: 'region', name: 'Region', width: 110, isSortable: true, }, { key: 'amount', name: 'Amount', width: 115, cellDataType: 'number', isSortable: true, formatter: ({ value }) => `$${Number(value).toLocaleString('en-US')}`, }, { key: 'status', name: 'Status', width: 105, }, ];
const grid = new Grid<OrderRow>({ container: stage, columnDefs, rowData: createOrders(35), getRowId: (row) => row.id, rowSelection: { mode: 'multiple', showCheckbox: true, checkboxPlacement: 'selectionColumn', checkboxPinned: 'start', selectOnRowClick: false, }, }); const stopTheme = syncGridTheme(stage);
const updateMeta = () => { const sel = grid.getSelectionState(); const count = 'rowIds' in sel ? sel.rowIds.length : 0; meta.textContent = `Selected rows: ${count}`; }; grid.addEventListener('selectionChanged', updateMeta);
addButton(controls, 'Select all', () => grid.selectAllRows('filtered')); addButton(controls, 'Clear', () => grid.clearRowSelection()); updateMeta();
return () => { grid.destroy(); stopTheme(); };}RowSelectionOptions
Section titled “RowSelectionOptions”| Option | Type | Default | Description |
|---|---|---|---|
mode |
'single' | 'multiple' |
— (required) | Single-row or multi-row selection |
selectOnRowClick |
boolean |
false |
Clicking a row toggles selection in addition to checkboxes |
showCheckbox |
boolean |
false |
Renders row selection checkboxes |
checkboxPlacement |
'column' | 'selectionColumn' |
'selectionColumn' |
Dedicated checkbox column or embedded inside an existing column (checkboxColId) |
checkboxColId |
string |
first column | Target column ID when checkboxPlacement: 'column' |
checkboxPinned |
'start' | 'end' |
'start' |
Pin edge for dedicated selectionColumn |
retainHiddenSelection |
boolean |
false |
Keeps selected IDs when rows are filtered out or hidden |
Programmatic API
Section titled “Programmatic API”grid.setRowSelected({ rowId } | { displayIndex }, selected)grid.isRowSelected(row)grid.selectAllRows('filtered' | 'page' | 'all')grid.clearRowSelection()grid.getSelectionState()/grid.applySelectionState(state)