Pular para o conteúdo

🎉 v5 is out! Head to the documentation to get started.

Data Grid - Components

The grid is highly customizable. Override components using the components prop.

Overriding components

As part of the customization API, the grid allows you to override internal components with the components prop. The prop accepts an object of type GridSlotsComponent.

If you wish to pass additional props in a component slot, you can do it using the componentsProps prop. This prop is of type GridSlotsComponentsProps.

As an example, you could override the column menu and pass additional props as below.

<DataGrid
  rows={rows}
  columns={columns}
  components={{
    ColumnMenu: MyCustomColumnMenu,
  }}
  componentsProps={{
    columnMenu: { background: 'red', counter: rows.length },
  }}
/>

Note: The casing is different between the components (ColumnMenu) and componentsProps (columnMenu) props.

Getting props

While overriding component slots, you might need to access the grid data. Therefore, the grid exposes a useGridSlotComponentProps hook which allows retrieving the following props.

  • state: the current grid state.
  • rows: the current rows in the grid.
  • columns: the current columns in the grid.
  • options: the current set of options in the grid.
  • apiRef: the GridApi ref that allows manipulating the grid.
  • rootElement: the root DOM element.

It can be used as below:

function CustomRowCounter() {
  const { rows } = useGridSlotComponentProps();

  return <div>Row count: {rows.length} </div>;
}

Components

The full list of overridable components can be found on the GridSlotsComponent API page.

Column menu

As mentioned above, the column menu is a component slot that can be recomposed easily and customized on each column as in the demo below.

default
name
stars
Open source
Material-UI
28000
Enterprise
DataGridPro
15000
Total Rows: 2

Below is the default GridColumnMenu.

export const GridColumnMenu = React.forwardRef<
  HTMLUListElement,
  GridColumnMenuProps
>(function GridColumnMenu(props: GridColumnMenuProps, ref) {
  const { hideMenu, currentColumn } = props;

  return (
    <GridColumnMenuContainer ref={ref} {...props}>
      <SortGridMenuItems onClick={hideMenu} column={currentColumn!} />
      <GridFilterMenuItem onClick={hideMenu} column={currentColumn!} />
      <HideGridColMenuItem onClick={hideMenu} column={currentColumn!} />
      <GridColumnsMenuItem onClick={hideMenu} column={currentColumn!} />
    </GridColumnMenuContainer>
  );
});

Toolbar

To enable the toolbar you need to add the Toolbar: GridToolbar to the grid components prop. This demo showcases how this can be achieved.

Desk
Commodity
Trader Name
Trader Email
Quantity

Linhas por página:

100

0-0 de 0

<DataGrid
  {...data}
  components={{
    Toolbar: GridToolbar,
  }}
/>

Alternatively, you can compose your own toolbar.

function CustomToolbar() {
  return (
    <GridToolbarContainer>
      <GridToolbarColumnsButton />
      <GridToolbarFilterButton />
      <GridToolbarDensitySelector />
      <GridToolbarExport />
    </GridToolbarContainer>
  );
}
Desk
Commodity
Trader Name
Trader Email
Quantity
D-1009
Rough Rice
Edith Graham
54,502
D-6707
Cocoa
Virgie Holloway
22,911
D-8132
Soybeans
Theresa Robertson
75,064
D-226
Cotton No.2
Violet Pope
96,554
D-1492
Soybean Meal
Clarence Powell
87,171
D-6327
Oats
Lina Warren
51,531
D-5248
Adzuki bean
Lester Armstrong
62,369
D-1977
Soybean Meal
Daisy Becker
67,084

Linhas por página:

100

1-10 de 10

<DataGrid
  {...data}
  components={{
    Toolbar: CustomToolbar,
  }}
/>

Footer

The grid exposes props to hide specific elements of the UI:

  • hideFooter: If true, the footer component is hidden.
  • hideFooterRowCount: If true, the row count in the footer is hidden.
  • hideFooterSelectedRowCount: If true, the selected row count in the footer is hidden.
  • hideFooterPagination: If true, the pagination component in the footer is hidden.
Status connected

Pagination

By default, pagination uses the TablePagination component that is optimized for handling tabular data. This demo replaces it with the Pagination component.

No rows
Desk
Commodity
Trader Name
Trader Email
Quantity
<DataGrid
  pagination
  pageSize={5}
  rowsPerPageOptions={[5]}
  components={{
    Pagination: CustomPagination,
  }}
  {...data}
/>

Loading overlay

By default, the loading overlay displays a circular progress. This demo replaces it with a linear progress.

Desk
Commodity
Trader Name
Trader Email
Quantity
D-3597
Rough Rice
Josie Medina
67,161
D-1480
Rough Rice
Daniel Farmer
38,671
D-8567
Coffee C
Ora Graham
75,107
D-9834
Sugar No.11
Clarence Curry
52,154
D-9866
Wheat
Delia Gross
16,761
D-9905
Milk
Evan Carter
23,339
D-7469
Soybean Meal
Virginia Becker
75,111
D-8694
Corn
Nancy Campbell
41,553
D-8016
Soybean Meal
Helena Holt
27,562
D-2707
Cocoa
Randy Little
1,351

Linhas por página:

100

1-100 de 100

<DataGrid
  components={{
    LoadingOverlay: CustomLoadingOverlay,
  }}
  loading
  {...data}
/>

No rows overlay

In the following demo, an illustration is added on top of the default "No Rows" message.

No Rows
Desk
Commodity
Trader Name
Trader Email
Quantity

Linhas por página:

100

0-0 de 0

<DataGrid
  components={{
    NoRowsOverlay: CustomNoRowsOverlay,
  }}
  rows={[]}
  columns={data.columns}
/>

Note: As the no rows overlay, the grid allows to override the no results overlay with the NoResultsOverlay slot.

Icons

As any component slot, every icon can be customized. However, it is not yet possible to use the componentsProps with icons.

name
1
stars
2
DataGrid
15000
Material-UI
28000

Linhas por página:

100

1-2 de 2

<DataGrid
  columns={columns}
  rows={rows}
  sortModel={[
    { field: 'name', sort: 'asc' },
    { field: 'stars', sort: 'desc' },
  ]}
  components={{
    ColumnSortedDescendingIcon: SortedDescendingIcon,
    ColumnSortedAscendingIcon: SortedAscendingIcon,
  }}
/>