Pular para o conteúdo

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

Data Grid - Rows

This section goes in details on the aspects of the rows you need to know.

Feeding data

Grid rows can be defined with the rows prop. rows expects an array of objects. Rows should have this type: GridRowData[]. The columns' "field" property should match a key of the row object (GridRowData).

name
React
Material-UI

Linhas por página:

100

1-2 de 2

<DataGrid
  columns={[{ field: 'name' }]}
  rows={[
    { id: 1, name: 'React' },
    { id: 2, name: 'Material-UI' },
  ]}
/>

Updating rows

Rows can be updated in two ways:

The rows prop

The simplest way is to provide the new rows using the rows prop. It replaces the previous values. This approach has some drawbacks:

  • You need to provide all the rows.
  • You might create a performance bottleneck when preparing the rows array to provide to the grid.

Infinite loading

The grid provides a onRowsScrollEnd prop that can be used to load additional rows when the scroll reaches the bottom of the viewport area.

In addition, the area in which the callback provided to the onRowsScrollEnd is called can be changed using scrollEndThreshold.

Desk
Commodity
Trader Name
Trader Email
Quantity
D-6076
Frozen Concentrated Orange Juice
Gerald Horton
10,128
D-6453
Cotton No.2
Tommy Powell
82,745
D-6764
Robusta coffee
Raymond Ramos
67,663
D-2986
Soybeans
Victoria Clayton
21,867
D-2453
Rough Rice
Kyle Lindsey
56,018
D-8545
Soybeans
Brett Warner
21,988
D-7081
Coffee C
Ray Drake
18,655
D-4054
Coffee C
Rosalie Bowen
41,942
D-172
Soybean Oil
Essie Castro
28,821
D-3264
Cotton No.2
Tom Phelps
95,810
Total Rows: 20
<DataGridPro
  {...data}
  rows={data.rows.concat(loadedRows)}
  loading={loading}
  hideFooterPagination
  onRowsScrollEnd={handleOnRowsScrollEnd}
  components={{
    LoadingOverlay: CustomLoadingOverlay,
  }}
/>

apiRef

The second way to update rows is to use the apiRef. This is an imperative API that is designed to solve the previous two limitations of the declarative rows prop. apiRef.current.updateRows(), updates the rows to the grid. It merges the new rows with the previous ones.

The following demo updates the rows every 200ms.

id
username
age
1
@hi
56
2
@araos
58
3
@tifmaufa
41
4
@pas
41
Total Rows: 4
<DataGridPro rows={rows} columns={columns} apiRef={apiRef} />

The default behavior of updateRows API is to upsert rows. So if a row has an id that is not in the current list of rows then it will be added to the grid.

Alternatively, if you would like to delete a row, you would need to pass an extra _action property in the update object as below.

apiRef.current.updateRows([{ id: 1, _action: 'delete' }]);

Row height

By default, the rows have a height of 52 pixels. This matches the normal height in the Material Design guidelines.

To change the row height for the whole grid, set the rowHeight prop:

Desk
Commodity
Trader Name
Trader Email
Quantity
D-4154
Frozen Concentrated Orange Juice
Nina Schwartz
93,193
D-745
Cocoa
Josephine Burke
44,710
D-4045
Sugar No.11
Micheal Dixon
58,484
D-2966
Adzuki bean
Harry Bradley
33,627
D-1765
Sugar No.11
Mike McLaughlin
7,874
D-4802
Cocoa
Dean Evans
81,826
D-8461
Coffee C
Edward Lawson
80,198
D-3261
Adzuki bean
Maggie Murray
6,258
D-3692
Sugar No.14
Owen Tate
2,916
D-5377
Robusta coffee
Lela Roberts
83,735
D-5162
Soybean Meal
Edgar Boone
49,025
D-1622
Corn
Katherine Robertson
43,802
D-3729
Adzuki bean
Mattie Crawford
89,401
D-3825
Milk
Henrietta Cain
1,421
D-3277
Cotton No.2
Stanley Russell
1,804
D-7855
Rapeseed
Ricardo McLaughlin
37,015
D-3353
Oats
Mason Walker
47,737
D-7643
Rapeseed
Caroline Curry
76,038
D-4419
Adzuki bean
Ora Stevens
90,900
D-1741
Adzuki bean
Fanny Ball
97,725
D-6535
Soybean Oil
Clifford Spencer
75,537
D-6263
Sugar No.14
Danny Gilbert
26,412

Linhas por página:

100

1-100 de 100

<DataGrid rowHeight={25} {...data} />

Styling rows

You can check the styling rows section for more information.

🚧 Row spanning

⚠️ This feature isn't implemented yet. It's coming.

👍 Upvote issue #207 if you want to see it land faster.

Each cell takes up the width of one row. Row spanning allows to change this default behavior. It allows cells to span multiple rows. This is very close to the "row spanning" in an HTML <table>.

🚧 Row reorder

⚠️ This feature isn't implemented yet. It's coming.

👍 Upvote issue #206 if you want to see it land faster.

Row reorder is used to rearrange rows by dragging the row with the mouse.

API