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
D-1539
Oats
Josie Daniel
77,726
D-8989
Cotton No.2
Victor Wise
66,329
D-6650
Adzuki bean
Frederick Boone
56,419
D-7453
Soybeans
Marc Mann
18,497
D-1819
Soybeans
Jorge Graves
27,762
D-9112
Cocoa
Eddie Chavez
4,153
D-2334
Wheat
Ray McCarthy
52,639
D-2840
Soybean Oil
Genevieve Clarke
28,981

Linhas por página:

100

1-100 de 100

<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-9495
Rapeseed
Vera Fletcher
94,215
D-3973
Corn
Calvin Fowler
67,571
D-3041
Coffee C
Garrett Austin
31,669
D-9405
Frozen Concentrated Orange Juice
Sean Haynes
79,655
D-7022
Robusta coffee
Clyde Ramirez
36,643
D-400
Soybean Meal
Darrell Porter
74,858
D-537
Oats
Catherine Sparks
97,185
D-429
Milk
Joshua Romero
24,561

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.
Avatar
Name
Website
Rating
Email
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.

Desk
Commodity
Trader Name
Trader Email
Quantity
D-1006
Rapeseed
Caleb Beck
90,181
D-7111
Soybean Meal
Terry Day
10,535
D-3982
Cotton No.2
Owen Ball
8,188
D-8743
Sugar No.14
Bill Love
7,667
D-3406
Oats
Effie Stevenson
31,187
<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-5178
Cocoa
Lida Reyes
98,069
D-7545
Sugar No.14
Max Bowman
90,040
D-384
Soybean Oil
Mollie Mendoza
56,231
D-4873
Soybeans
Marion Francis
48,057
D-6723
Robusta coffee
Georgie Curry
61,209
D-3441
Oats
Jeremy Turner
18,858
D-9766
Oats
Eliza Ingram
13,626
D-6230
Wheat
Christina Quinn
23,880
D-1926
Adzuki bean
Ronnie Allison
31,794
D-8579
Adzuki bean
Steven Harmon
59,407

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,
  }}
/>