Data Grid - Events
The data grid emits events that can be subscribed to attach custom behavior.
Subscribing to events
You can subscribe to one of the events emitted by calling apiRef.current.subscribeEvent() with the name of the event and a handler. The handler will be called with three arguments:
- an object with information related to the event
- a
MuiEventcontaining the DOM event or the React synthetic event, when available - a
GridCallbackDetailscontaining theGridApionly ifDataGridProis being used.
/**
* Allows to register a handler for an event.
* @param event The name of event
* @param handler The handler to be called
* @param options Additional options for this listener
* @returns A function to unsubscribe from this event
*/
subscribeEvent: (
event: GridEventsStr,
handler: (params: any, event: MuiEvent, details: GridCallbackDetails) => void,
options?: EventListenerOptions,
) => () => void;
The following demo shows how to subscribe to the columnResize event. Try it by resizing the columns.
<div style={{ height: 180, width: '100%' }}>
<DataGridPro apiRef={apiRef} {...data} />
</div>
{message && (
<Alert severity="info" style={{ marginTop: 8 }}>
{message}
</Alert>
)}Disabling the default behavior
Depending on the use case, it might be necessary to disable the default action taken by an event.
The MuiEvent passed to the event handler has a defaultMuiPrevented property to control when the default behavior can be executed or not.
Set it to true to block the default handling of an event and implement your own.
<DataGrid
onCellClick={(params: GridCellParams, event: MuiEvent<React.MouseEvent>) => {
event.defaultMuiPrevented = true;
}}
/>
Usually, double clicking a cell will put it into edit mode. The following example changes this behavior by also requiring Ctrl to be pressed.
<DataGrid
onCellDoubleClick={(params, event) => {
if (!event.ctrlKey) {
event.defaultMuiPrevented = true;
}
}}
{...data}
/>| Name | Description | ||
|---|---|---|---|
cellClick | Fired when a cell is clicked. Params: GridCellParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
cellDoubleClick | Fired when a cell is double-clicked. Params: GridCellParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
cellEditCommit | Fired when the props of the edit input are committed. Params: GridCellEditCommitParamsEvent: MuiEvent<MuiBaseEvent> | ||
cellEditStart | Fired when the cell turns to edit mode. Params: GridCellEditStartParamsEvent: MuiEvent<React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>> | ||
cellEditStop | Fired when the cell turns back to view mode. Params: GridCellEditStopParamsEvent: MuiEvent<MuiBaseEvent> | ||
cellFocusIn | Fired when a cell gains focus. Params: GridCellParamsEvent: MuiEvent<{}> | ||
cellFocusOut | Fired when a cell loses focus. Params: GridCellParamsEvent: MuiEvent<MuiBaseEvent> | ||
cellKeyDown | Fired when a keydown event happens in a cell.Params: GridCellParamsEvent: MuiEvent<React.KeyboardEvent<HTMLElement>> | ||
cellModesModelChange | Fired when the model that controls the cell modes changes. Params: GridCellModesModelEvent: MuiEvent<{}> | ||
cellMouseDown | Fired when a mousedown event happens in a cell.Params: GridCellParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
cellMouseUp | Fired when a mouseup event happens in a cell.Params: GridCellParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
columnHeaderClick | Fired when a column header is clicked Params: GridColumnHeaderParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
columnHeaderDoubleClick | Fired when a column header is double-clicked. Params: GridColumnHeaderParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
columnHeaderKeyDown | Fired when a key is pressed in a column header. It's mapped do the keydown DOM event.Params: GridColumnHeaderParamsEvent: MuiEvent<React.KeyboardEvent<HTMLElement>> | ||
columnOrderChange | Fired when the user ends reordering a column. Params: GridColumnOrderChangeParamsEvent: MuiEvent<{}> | ||
columnResize | Fired during the resizing of a column. Params: GridColumnResizeParamsEvent: MuiEvent<MouseEvent> | ||
columnResizeStart | Fired when the user starts resizing a column. Params: { field: string }Event: MuiEvent<React.MouseEvent<HTMLElement>> | ||
columnResizeStop | Fired when the user stops resizing a column. Params: nullEvent: MuiEvent<MouseEvent> | ||
columnsChange | Fired when the columns state is changed. Params: string[]Event: MuiEvent<{}> | ||
columnVisibilityChange | Fired when a column visibility changes.
It is not fired when the columnVisibilityModel is controlled or initialized.
It is not fired when toggling all column's visibility at once.Params: GridColumnVisibilityChangeParamsEvent: MuiEvent<{}> | ||
columnVisibilityModelChange | Fired when the column visibility model changes. Params: GridColumnVisibilityModelEvent: MuiEvent<{}> | ||
columnWidthChange | Fired when the width of a column is changed. Params: GridColumnResizeParamsEvent: MuiEvent<MouseEvent | {}> | ||
componentError | Fired when an exception is thrown in the grid. Params: anyEvent: MuiEvent<{}> | ||
debouncedResize | Fired when the grid is resized with a debounced time of 60ms. Params: ElementSizeEvent: MuiEvent<{}> | ||
editCellPropsChange | Fired when the props of the edit cell changes. Params: GridEditCellPropsParamsEvent: MuiEvent<React.SyntheticEvent<HTMLElement> | {}> | ||
editRowsModelChange | Fired when the row editing model changes. Params: GridEditRowsModelEvent: MuiEvent<{}> | ||
filterModelChange | Fired when the filter model changes. Params: GridFilterModelEvent: MuiEvent<{}> | ||
headerSelectionCheckboxChange | Fired when the value of the selection checkbox of the header is changed Params: GridHeaderSelectionCheckboxParamsEvent: MuiEvent<{}> | ||
menuClose | Fired when the grid menu is closed. Params: GridMenuParamsEvent: MuiEvent<{}> | ||
menuOpen | Fired when the menu is opened. Params: GridMenuParamsEvent: MuiEvent<{}> | ||
pageChange | Fired when the page changes. Params: numberEvent: MuiEvent<{}> | ||
pageSizeChange | Fired when the page size changes. Params: numberEvent: MuiEvent<{}> | ||
preferencePanelClose | Fired when the preference panel is closed. Params: GridPreferencePanelParamsEvent: MuiEvent<{}> | ||
preferencePanelOpen | Fired when the preference panel is opened. Params: GridPreferencePanelParamsEvent: MuiEvent<{}> | ||
resize | Fired when the grid is resized. Params: ElementSizeEvent: MuiEvent<{}> | ||
rowClick | Fired when a row is clicked.
Not fired if the cell clicked is from an interactive column (actions, checkbox, etc). Params: GridRowParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
rowDoubleClick | Fired when a row is double-clicked. Params: GridRowParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
rowEditCommit | Fired when the props of the edit input are committed. Params: GridRowIdEvent: MuiEvent<MuiBaseEvent> | ||
rowEditStart | Fired when the row turns to edit mode. Params: GridRowEditStartParamsEvent: MuiEvent<React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>> | ||
rowEditStop | Fired when the row turns back to view mode. Params: GridRowEditStopParamsEvent: MuiEvent<MuiBaseEvent> | ||
rowGroupingModelChange | Fired when the row grouping model changes. Params: GridRowGroupingModelEvent: MuiEvent<{}> | ||
rowModesModelChange | Fired when the model that controls the row modes changes. Params: GridRowModesModelEvent: MuiEvent<{}> | ||
rowMouseEnter | Fired when the mouse enters the row. Called with a GridRowParams object. Params: GridRowParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
rowMouseLeave | Fired when the mouse leaves the row. Called with a GridRowParams object. Params: GridRowParamsEvent: MuiEvent<React.MouseEvent<HTMLElement>> | ||
rowOrderChange | Fired when the user ends reordering a row. Params: GridRowOrderChangeParamsEvent: MuiEvent<{}> | ||
rowSelectionCheckboxChange | Fired when the value of the selection checkbox of a row is changed Params: GridRowSelectionCheckboxParamsEvent: MuiEvent<React.ChangeEvent<HTMLElement>> | ||
rowsScroll | Fired during the scroll of the grid viewport. Params: GridScrollParamsEvent: MuiEvent<React.UIEvent | MuiBaseEvent> | ||
rowsScrollEnd | Fired when scrolling to the bottom of the grid viewport. Params: GridRowScrollEndParamsEvent: MuiEvent<{}> | ||
selectionChange | Fired when the selection state of one or multiple rows changes. Params: GridSelectionModelEvent: MuiEvent<{}> | ||
sortModelChange | Fired when the sort model changes. Params: GridSortModelEvent: MuiEvent<{}> | ||
stateChange | Fired when the state of the grid is updated. Params: anyEvent: MuiEvent<{}> | ||
unmount | Fired when the grid is unmounted. Event: MuiEvent<{}> | ||
viewportInnerSizeChange | Fired when the inner size of the viewport changes. Called with an ElementSize object. Params: ElementSizeEvent: MuiEvent<{}> | ||