Skip to content

Cell Selection

Enable cellSelection for Excel-like cell and rectangular range selection. Ranges work with keyboard (Shift + Arrows), pointer drag, and clipboard copy/paste.

Multi-Range Cell Selection
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,
},
{
key: 'customer',
name: 'Customer',
width: 175,
flex: 1,
},
{
key: 'region',
name: 'Region',
width: 105,
},
{
key: 'quantity',
name: 'Qty',
width: 80,
cellDataType: 'number',
},
{
key: 'amount',
name: 'Amount',
width: 115,
cellDataType: 'number',
formatter: ({ value }) =>
`$${Number(value).toLocaleString('en-US')}`,
},
];
const grid = new Grid<OrderRow>({
container: stage,
columnDefs,
rowData: createOrders(30),
getRowId: (row) => row.id,
cellSelection: {
mode: 'range',
maxRanges: 5,
},
});
const stopTheme = syncGridTheme(stage);
grid.setCellRange(
{
row: { displayIndex: 1 },
colId: 'customer',
},
{
row: { displayIndex: 3 },
colId: 'quantity',
},
);
const updateMeta = () => {
const sel = grid.getSelectionState();
const ranges = 'ranges' in sel ? sel.ranges.length : 0;
meta.textContent = `Active ranges: ${ranges}`;
};
grid.addEventListener('selectionChanged', updateMeta);
addButton(controls, 'Add range', () => {
grid.setCellRange(
{
row: { displayIndex: 5 },
colId: 'region',
},
{
row: { displayIndex: 7 },
colId: 'amount',
},
true,
);
});
addButton(controls, 'Clear', () => grid.clearCellSelection());
updateMeta();
return () => {
grid.destroy();
stopTheme();
};
}
Option Type Default Description
mode 'single' | 'range' — (required) Single cell or rectangular multi-cell range selection
maxRanges number 10 Maximum concurrent disjoint ranges
// Replace ranges (add = false) or append another range (add = true)
grid.setCellRange(
{ row: { displayIndex: 1 }, colId: 'customer' },
{ row: { displayIndex: 3 }, colId: 'quantity' },
true,
);
grid.clearCellSelection();
grid.clearSelection(); // clears both row and cell selections
  • Pivot Mode: Use grid.setPivotCellRange(anchor, focus, add?) for pivot result cells; getSelectionState() returns SelectionStateV3 (version: 3) in pivot mode and SelectionState (version: 2) otherwise.