View as Markdown

Selection

Selection

Row, cell and column selection with pointer, keyboard and methods; id-based, persistent and virtualization-safe.

Selection is on by default as single row selection; selection={false} turns it off, and an object configures it. It is stored by row id and column id, never by view index, so virtualization and live data changes never shift it to another row.

import { DataGrid } from '@salmonui/data-grid';
import '@salmonui/data-grid/styles.css';

<DataGrid
  aria-label="Orders"
  data={orders}
  columns={[{ field: 'select', type: 'selection' }, ...columns]}
  selection={{ multiple: true, persistent: true, rowIds: selectedIds }}
  onSelectionChange={(event) => setSelectedIds(event.rowIds)}
/>

Rows

multiple (default false) switches from single to multiple selection. Ctrl/⌘+click toggles a row and Shift+click selects a range. A checkbox column (a column with type: 'selection') adds a header select-all checkbox; checkboxOnly makes it the only way to select, and checkboxRowClick: 'replace' makes a plain row click replace the checkbox selection instead of toggling the row ('add', the default). With toggle (default true) clicking the only selected row deselects it, and additive toggles rows without a modifier.

By default a sort, filter, search, grouping or page change clears the selection. persistent: true keeps it across those changes (this also works for cells and does not need multiple selection).

Initial and controlled selection use row ids: selection.rowIds is reactive, so a changed array re-applies. There is no selection by row index, on purpose: an index is invalidated by every sort, filter, page or live update, while an id is not. To select by position, map the index to the id of that row (api.getViewRows()[index] through getRowId or the primaryKey column) and pass it in rowIds.

api.selectAll() selects every filtered row in O(1): it stores a scope plus excluded ids, which serializes and survives paging; selectAll({ scope: 'page' }) selects the current page only.

Methods select rows or id ranges (selectRows(rowIds, { mode }), selectRowRange(fromRowId, toRowId)), clear rows, cells or columns (clearSelection({ rows, cells, columns })), and read the selection (getSelectedRows(), getSelectedRowIds(), isRowSelected(), and the serializable getSelection() / setSelection()). The cancelable onBeforeSelectionChange and onSelectionChange report the selected rowIds, the added and removed ids, rows(), the input target and the scope. Rows and cells expose aria-selected.

Row selection

Open demo

Single and multiple row selection, the type: 'selection' checkbox column with select-all, checkbox-only and replace-on-row-click modes, toggle, additive clicks, persistence across filtering, cancelable changes and the selection methods.

Order
Customer
Country
Amount
Shipped
ORD-10000
Aiko Tan
France
$75.86
ORD-10001
Hana Sato
Germany
$107.10
ORD-10002
Bao Nguyen
Japan
$612.75
ORD-10003
Farah Aziz
Malaysia
$519.20
ORD-10004
Goran Petrov
Malaysia
$55.50
ORD-10005
Chen Wei
Japan
$105.45
ORD-10006
Jun Park
Japan
$74.00
ORD-10007
Chen Wei
Japan
$130.90
ORD-10008
Aiko Tan
France
$22.95
ORD-10009
Dewi Lestari
Germany
$17.58
ORD-10010
Chen Wei
Japan
$16.65
ORD-10011
Farah Aziz
Germany
$68.85
ORD-10012
Hana Sato
Germany
$23.80
ORD-10013
Jun Park
Japan
$130.05
ORD-10014
Aiko Tan
France
$519.20
ORD-10015
Hana Sato
Malaysia
$367.65
ORD-10016
Farah Aziz
France
$348.30
ORD-10017
Jun Park
Malaysia
$111.00
ORD-10018
Chen Wei
Malaysia
$645.00
ORD-10019
Goran Petrov
Japan
$59.50
No events yet.

Cells and columns

mode: 'cell', or row plus cell mode mode: 'both', selects cellRange: 'flow' (text-like, the default) or 'box' / 'boxWithBorder' (rectangular) ranges; columns: true adds column selection. selectCells(cells, { mode }), selectCellRange(anchor, focus) and selectColumns(fields, { mode }) select from code, and getSelectedCells() / getSelectedColumns() read the result.

The keyboard map (Mod is Ctrl, or ⌘ on macOS):

Keys Action
Arrow keys Move focus
Shift+Arrow keys Extend the selection
Enter / Shift+Enter Move to the cell below / above and select it
Space Toggle the focused row or cell
Shift+Space Select the focused row
Ctrl+Space Toggle the focused column (with columns: true)
Mod+A Select every row in view, or every cell in cell mode (multiple selection)
Mod+Shift+A Select every filtered row (multiple selection)
Escape Clear the selection
Cell and column changes raise the same cancelable
onBeforeSelectionChange and onSelectionChange events as rows:
target is 'cell' or 'column', and addedCells / removedCells /
addedColumns / removedColumns list what changed. In row plus cell mode a
click raises a row event and a cell event. Ranges are stored as anchors, so a range can span rows and
columns that are not rendered, so cell selection works with
virtualization.

Cell and column selection

Open demo

Cell and row-plus-cell modes, flow and box cell ranges, column selection, keyboard selection and range selection across virtualized rows and columns.

#
Value 1
Value 2
Text 3
Value 4
Value 5
Text 6
Value 7
Value 8
Text 9
Value 10
Value 11
Text 12
Value 13
Value 14
Text 15
Value 16
Value 17
Text 18
Value 19
0
17,595
73,603
india-0
89,904
53,726
juliet-0
51,543
54,681
bravo-0
68,199
6,896
juliet-0
8,919
42,947
alpha-0
29,833
3,953
charlie-0
59,401
1
19,690
76,900
golf-1
63,257
93,463
charlie-1
73,180
76,755
delta-1
66,097
4,541
bravo-1
17,543
8,514
echo-1
35,140
25,990
juliet-1
39,531
2
59,295
35,017
charlie-2
9,491
78,361
echo-2
97,760
84,967
charlie-2
33,456
62,943
alpha-2
96,517
65,225
foxtrot-2
22,349
49,286
charlie-2
6,173
3
35,877
55,968
alpha-3
7,608
53,800
foxtrot-3
23,732
38,972
charlie-3
25,902
12,898
echo-3
46,117
4,168
alpha-3
52,853
77,546
alpha-3
86,219
4
34,311
61,050
hotel-4
22,377
63,353
bravo-4
74,018
13,961
india-4
56,331
42,367
foxtrot-4
45,663
88,442
charlie-4
55,558
63,841
juliet-4
44,597
5
91,410
88,735
india-5
4,726
46,755
foxtrot-5
18,912
53,583
foxtrot-5
52,092
75,215
india-5
37,385
87,851
charlie-5
65,715
66,738
charlie-5
57,729
6
19,681
45,767
juliet-6
81,684
43,403
delta-6
67,496
63,555
hotel-6
84,296
25,267
charlie-6
32,699
84,878
juliet-6
90,775
18,986
charlie-6
26,037
7
54,528
24,961
bravo-7
38,898
11,936
bravo-7
24,376
70,956
india-7
13,850
376
foxtrot-7
21,286
80,591
foxtrot-7
29,490
66,515
hotel-7
56,516
8
60,869
8,446
charlie-8
75,901
69,947
juliet-8
51,390
69,049
juliet-8
56,014
71,245
charlie-8
72,266
55,207
india-8
6,795
52,120
charlie-8
8,427
9
75,855
9,597
alpha-9
47,126
22,101
foxtrot-9
88,095
69,323
hotel-9
28,224
13,727
bravo-9
50,639
78,507
india-9
34,637
37,497
foxtrot-9
1,229
10
58,099
68,722
bravo-10
13,363
50,105
charlie-10
8,700
37,059
delta-10
89,294
81,549
bravo-10
14,399
84
delta-10
41,206
1,699
delta-10
28,123
11
44,095
65,059
charlie-11
27,571
63,118
echo-11
76,276
46,620
charlie-11
85,845
43,610
echo-11
61,207
22,517
india-11
52,089
96,035
hotel-11
79,839
12
87,775
14,793
echo-12
53,666
21,185
charlie-12
71,766
90,493
charlie-12
81,970
56,085
golf-12
76,381
86,883
india-12
50,433
28,788
golf-12
43,415
13
11,378
92,348
juliet-13
50,530
91,535
foxtrot-13
46,779
58,126
hotel-13
74,472
75,706
delta-13
20,782
93,092
foxtrot-13
59,582
68,970
charlie-13
24,546
14
50,747
60,702
delta-14
13,751
21,341
india-14
404
12,918
foxtrot-14
31,446
39,169
foxtrot-14
80,323
80,536
foxtrot-14
74,750
82,895
foxtrot-14
70,528
15
66,303
27,174
foxtrot-15
15,685
41,810
charlie-15
63,343
80,874
echo-15
15,679
9,656
hotel-15
39,314
48,146
bravo-15
39,322
69,568
hotel-15
5,880
16
96,820
6,701
foxtrot-16
19,607
7,853
alpha-16
71,448
63,714
echo-16
38,877
60,995
delta-16
5,792
75,559
juliet-16
73,833
86,485
india-16
32,670
17
60,534
72,931
alpha-17
68,578
91,404
charlie-17
32,260
45,307
foxtrot-17
93,320
87,659
alpha-17
29,270
59,959
alpha-17
439
77,734
alpha-17
33,653
18
91,214
23,934
india-18
51,017
88,074
echo-18
67,009
27,532
hotel-18
78,073
35,746
juliet-18
8,896
53,619
charlie-18
48,670
26,279
hotel-18
88,194
19
31,831
70,702
juliet-19
96,175
27,610
india-19
96,357
50,732
india-19
87,297
19,078
delta-19
49,402
72,108
bravo-19
918
73,850
juliet-19
33,098

Click a cell, then Shift+click or Shift+Arrow to extend; Ctrl/⌘+click adds cells; Ctrl/⌘+A selects all; Escape clears.

No events yet.

Range fill

With editing.fillHandle and cell selection, the cell at the bottom-right of the selected range shows a small square, the fill handle. Dragging it down, up, left or right extends the range along that one axis (a diagonal drag follows the axis the pointer has moved farther along) and, on release, fills the new cells by tiling the source range: a two-row source repeats every two rows. A dashed outline previews the extended range, the grid auto-scrolls while the pointer is held at or past the body edge, and Escape cancels the drag. The drag never touches React state: the preview and the auto-scroll are DOM writes, and the grid renders once, when the fill is applied. api.fillSelection(rowId, field) is the programmatic form of the same action, and onBeforeFill (range, target) cancels either with preventDefault(). Filled cells go through the edit pipeline, so parsing, validation, saving and undo apply as for typed edits, and pending drafts of the source range are copied as shown.

<DataGrid
  selection={{ mode: 'cell', multiple: true, cellRange: 'box' }}
  editing={{ mode: 'cell', fillHandle: true, undo: true }}
/>;
api.selectCellRange({ rowId: 'ORD-10000', field: 'customer' }, { rowId: 'ORD-10001', field: 'product' });
api.fillSelection('ORD-13999', 'product');

The fill addresses cells by row id and walks the full row model, not the rendered window, so it works with row virtualization and fills rows that are not rendered, including rows the drag scrolled past. Hold the pointer below or above the body while dragging and the grid auto-scrolls; the rows that scroll into view join the fill, and so do the rows that scroll out again. The drag also crosses pin edges, from a start-pinned column into the scrolling columns or from them into an end-pinned column. Read-only cells and the key column are skipped.

Range fill

Open demo

Drag the fill handle (or call api.fillSelection()) to tile the selected cell range over 5,000 virtualized rows, including rows far outside the rendered window, with undo.

Order
Customer
Product
Qty
Note
ORD-10000
Jun Park
Oolong tea
9
ORD-10001
Hana Sato
Oolong tea
11
Leave the parcel with the building reception if nobody answers the door.
ORD-10002
Jun Park
Filter beans
5
Call before delivery
ORD-10003
Chen Wei
Green tea
12
ORD-10004
Dewi Lestari
Kettle
7
Leave the parcel with the building reception if nobody answers the door.
ORD-10005
Farah Aziz
Espresso beans
2
Gift wrap
ORD-10006
Goran Petrov
Grinder
5
Gift wrap
ORD-10007
Jun Park
Oolong tea
8
ORD-10008
Bao Nguyen
Pour-over kit
5
Leave the parcel with the building reception if nobody answers the door.
ORD-10009
Bao Nguyen
Oolong tea
11
ORD-10010
Ines Duarte
Green tea
9
Gift wrap
ORD-10011
Bao Nguyen
Kettle
11
ORD-10012
Elif Kaya
Oolong tea
8
Call before delivery
ORD-10013
Farah Aziz
Green tea
8
Leave the parcel with the building reception if nobody answers the door.
ORD-10014
Bao Nguyen
Green tea
10
ORD-10015
Goran Petrov
Espresso beans
9
ORD-10016
Bao Nguyen
Espresso beans
9
ORD-10017
Jun Park
Filter beans
12
ORD-10018
Elif Kaya
Pour-over kit
9
Leave the parcel with the building reception if nobody answers the door.
ORD-10019
Jun Park
Espresso beans
6
Gift wrap

Select a cell or a range, then drag its fill handle or use the buttons.

API reference

DataGridSelection lists the selection options, DataGridSelectionChangeEvent and DataGridBeforeFillEvent the event payloads, and DataGridApi the selection methods.