View as Markdown

Events

Events

The event model - onReady, before-event and after-event pairs, header attributes, and the interaction events of selection, resizing, column drag and the menus.

Every grid event is an on* prop. Actions come in pairs: onBefore<Action> runs first and cancels the action with event.preventDefault(), then on<Action> reports the committed change. Each event object carries type, source ('pointer', 'keyboard', 'touch' or 'api') and defaultPrevented. An event object is allocated only when its handler is bound, and scrolling never raises events.

Event objects are plain snapshots, never the grid's internal store objects; in development builds they are frozen, the onKeyDown context included; production builds skip the freeze. Read them, but do not assign to their properties. The one exception is the items array of a menu before-event: change it in place (items.splice(...), items.push(...)); reassigning event.items throws in development.

Lifecycle

onReady(api) fires once after the first commit of each mount, with the grid instance; it is the place for startup calls into the API. When the grid uses a feature that loads on demand (for example editing), onReady waits for it. When the grid unmounts, the data source is disconnected and the in-flight read is aborted.

Before and after events

There are no generic actionBegin / actionComplete / actionFailure events. Each action has its own pair with a typed payload: onBeforeSortChange / onSortChange ({ sortBy, previousSortBy, field }), onBeforeFilterChange / onFilterChange, onBeforeSearchChange / onSearchChange, onBeforePageChange / onPageChange, onBeforeGroupChange / onGroupChange, and so on. Failures arrive in onError ({ error, action }); a failed data load reports action: 'data'. In the demo, the checkbox cancels every sort in onBeforeSortChange, so onSortChange never follows.

Typed pairs keep each payload specific instead of a request-type switch over one shared argument, and an event object is allocated only for a bound handler. Cancel an action with event.preventDefault() in its onBefore... handler, and handle failures in onError, whose action is 'data', 'save', 'persistState' or the rejected command's feature:

<DataGrid
  data={orders}
  columns={columns}
  sorting
  onBeforeSortChange={(event) => {
    if (event.field === 'CustomerName') event.preventDefault();
  }}
  onError={(event) => console.error(event.action, event.error)}
/>

Header attributes

The column's headerProps put a class, a style, a title and data-* attributes on the header cell. The Amount header in the demo carries a tooltip and a data-unit attribute. Per-row and per-cell attributes are getRowProps and getCellProps (templates).

Interaction events

Each feature documents its own events: selection (onBeforeSelectionChange / onSelectionChange with added, removed and rowIds), editing (editing), export and print (export). Column resizing fires onColumnResizeStart (cancellable), onColumnResize per frame and onColumnResizeEnd; column drag fires onColumnDragStart, onColumnDrag and onColumnDrop, then the reorder pair onBeforeColumnReorder / onColumnReorder; row drag fires onRowDragStart, onRowDrag, onBeforeRowDrop and onRowDrop (row drag and drop). Per-frame events fire at most once per animation frame and never touch React state inside the grid.

The menus open through before-events that can cancel them or change their items: onBeforeColumnMenuOpen / onColumnMenuItemClick for the column menu and onBeforeContextMenuOpen / onContextMenuItemClick for the context menu, whose target tells a header, a cell, a row, a group caption or the pager apart.

Data Grid events

Open demo

onReady on every mount, a cancellable before-event pair, headerProps, and selection, resize, column drag, column menu and context menu events in one on-page log.

Order
Customer
Country
Amount
ORD-10000
Hana Sato
Germany
$84.79
ORD-10001
Aiko Tan
Portugal
$928.80
ORD-10002
Elif Kaya
Malaysia
$246.62
ORD-10003
Jun Park
Germany
$467.28
ORD-10004
Chen Wei
Malaysia
$857.85
ORD-10005
Dewi Lestari
Germany
$369.93
ORD-10006
Elif Kaya
Germany
$54.19
ORD-10007
Chen Wei
Portugal
$657.90
ORD-10008
Elif Kaya
Malaysia
$616.55
ORD-10009
Dewi Lestari
France
$94.35
ORD-10010
Goran Petrov
Japan
$21.68
ORD-10011
Jun Park
Malaysia
$678.21
ORD-10012
Goran Petrov
Japan
$16.65
ORD-10013
Jun Park
Germany
$79.80
ORD-10014
Farah Aziz
Japan
$87.88
ORD-10015
Jun Park
Portugal
$141.53
ORD-10016
Dewi Lestari
France
$645.00
ORD-10017
Bao Nguyen
Japan
$79.80
ORD-10018
Aiko Tan
France
$778.80
ORD-10019
Chen Wei
France
$1,290.00
No events yet.

Row and cell clicks

onRowClick and onRowDoubleClick receive the rowId, the row, its visible rowIndex, the clicked field and column (null outside a cell), the row element and the nativeEvent. onCellClick and onCellDoubleClick add the cell value and pass the cell element; the cell event fires before the row event. They fire for data rows only, not for group captions, aggregate footers or detail rows. preventDefault() on a click skips the built-in selection and focus; on a double click it skips the edit start. The grid attaches one delegated listener per event type on the body, and only while one of these handlers is bound. There is no context-menu twin: right clicks go through onBeforeContextMenuOpen.

Row and cell click events

Open demo

onCellClick, onRowClick, onCellDoubleClick and onRowDoubleClick with the row, the column and the cell value; preventDefault() skips the built-in selection or edit start.

Order
Customer
Country
Amount
ORD-10000
Ines Duarte
France
$438.60
ORD-10001
Goran Petrov
Japan
$114.75
ORD-10002
Bao Nguyen
Japan
$1,315.80
ORD-10003
Chen Wei
Japan
$33.30
ORD-10004
Dewi Lestari
Malaysia
$96.66
ORD-10005
Jun Park
France
$168.00
ORD-10006
Farah Aziz
Malaysia
$43.94
ORD-10007
Farah Aziz
Germany
$37.80
ORD-10008
Dewi Lestari
France
$767.55
ORD-10009
Elif Kaya
Malaysia
$148.00
ORD-10010
Ines Duarte
France
$61.66
ORD-10011
Dewi Lestari
Japan
$9.25
ORD-10012
Ines Duarte
Malaysia
$525.69
ORD-10013
Farah Aziz
Japan
$103.28
ORD-10014
Hana Sato
Malaysia
$61.51
ORD-10015
Jun Park
Malaysia
$142.80
ORD-10016
Bao Nguyen
Malaysia
$140.25
ORD-10017
Aiko Tan
Malaysia
$116.82
ORD-10018
Hana Sato
Portugal
$54.19
ORD-10019
Aiko Tan
Japan
$35.70
No events yet.

API reference

Every event prop is listed on DataGridProps, with its payload type (for example DataGridSortChangeEvent, DataGridRowClickEvent and DataGridErrorEvent).