View as Markdown

Templates

Templates

Cell, header and row templates, filter, pager, toolbar, editor and empty templates, per-row and per-cell attribute hooks, and the rules templates follow.

Templates are functions that return React nodes. String templates are not supported; React escapes values by default.

Cell and header templates

A column renderCell receives a context with the row, its rowId and view rowIndex, the column, the raw value, the formattedValue, and the selected and editing flags. A column renderHeader receives { column, sortDirection, sortIndex, filtered, grouped } and replaces the header content while sorting, resizing and reordering affordances stay in place.

const columns: DataGridColumn<Order>[] = [
  {
    field: 'status',
    header: 'Status',
    renderCell: ({ value }) => <StatusBadge status={value as Status} />,
    renderHeader: ({ column, filtered }) => <>{column.header}{filtered ? ' (filtered)' : ''}</>,
    cellClassName: ({ value }) => (value === 'late' ? 'is-late' : undefined),
  },
];

Cells and headers accept extra attributes and class names: column cellProps and cellClassName take a value or a function of the cell context, and headerProps a value. The grid-level hooks getRowProps(row, rowIndex), rowClassName(row, rowIndex) and getCellProps(ctx) (the equivalents of rowDataBound and queryCellInfo) return props instead of mutating the DOM, and run only for rendered rows and cells, so their cost is bounded by the viewport, not the data size.

Cell and header templates

Open demo

Function templates for cells and headers, per-cell attributes and classes, and per-row and per-cell hooks that run only for rendered rows.

Order
Customer
Priority tpl
Amount
City
ORD-10000
Dewi Lestari #1
High
$986.85
Kuala Lumpur
ORD-10001
Bao Nguyen #2
High
$63.75
Lyon
ORD-10002
Bao Nguyen #3
Low
$53.20
Reims
ORD-10003
Aiko Tan #4
High
$431.59
Osaka
ORD-10004
Chen Wei #5
Low
$264.60
Reims
ORD-10005
Elif Kaya #6
Normal
$551.65
Reims
ORD-10006
Bao Nguyen #7
Low
$1,044.90
Lyon
ORD-10007
Elif Kaya #8
Low
$185.00
Munich
ORD-10008
Bao Nguyen #9
Low
$47.18
Porto
ORD-10009
Bao Nguyen #10
Normal
$94.35
Reims
ORD-10010
Hana Sato #11
Normal
$114.75
Munich
ORD-10011
Dewi Lestari #12
Normal
$74.93
Munich
ORD-10012
Chen Wei #13
Low
$857.85
Porto
ORD-10013
Bao Nguyen #14
High
$55.17
Penang
ORD-10014
Hana Sato #15
High
$99.90
Munich
ORD-10015
Farah Aziz #16
Low
$8.79
Osaka
ORD-10016
Aiko Tan #17
Normal
$159.60
Kuala Lumpur
ORD-10017
Farah Aziz #18
High
$149.85
Lyon
ORD-10018
Chen Wei #19
High
$61.66
Reims
ORD-10019
Goran Petrov #20
Normal
$114.75
Reims

Row templates

renderRow replaces the whole row element. Its context is { row, rowId, rowIndex, defaultRow, rowProps, cells }: the template must spread rowProps, which carry role="row", the ARIA row index and the row id, and place cells inside; defaultRow is the built-in row, to wrap instead of replace. Row templates work with row virtualization.

Row template with virtualization

Open demo

A row template over 20,000 virtualized rows that keeps ARIA row and gridcell semantics; unshipped orders are flagged.

Order
Customer
Product
Qty
Amount
Shipped
ORD-10000
Dewi Lestari
Green tea
1
$7.86
true
ORD-10001
Aiko Tan
Espresso beans
6
$94.35
false
ORD-10002
Goran Petrov
Oolong tea
12
$137.70
false
ORD-10003
Goran Petrov
Espresso beans
12
$210.90
true
ORD-10004
Hana Sato
Filter beans
3
$35.70
true
ORD-10005
Hana Sato
Kettle
10
$649.00
false
ORD-10006
Ines Duarte
Kettle
1
$61.66
false
ORD-10007
Goran Petrov
Kettle
1
$64.90
true
ORD-10008
Goran Petrov
Filter beans
2
$28.00
true
ORD-10009
Farah Aziz
Oolong tea
5
$60.56
true
ORD-10010
Aiko Tan
Espresso beans
9
$141.53
false
ORD-10011
Dewi Lestari
Green tea
3
$23.59
false
ORD-10012
Hana Sato
Grinder
7
$767.55
false
ORD-10013
Jun Park
Kettle
6
$330.99
true
ORD-10014
Dewi Lestari
Filter beans
8
$106.40
true
ORD-10015
Elif Kaya
Green tea
8
$74.00
false
ORD-10016
Dewi Lestari
Grinder
4
$464.40
false
ORD-10017
Aiko Tan
Filter beans
6
$71.40
false
ORD-10018
Chen Wei
Grinder
9
$1,102.95
true
ORD-10019
Farah Aziz
Green tea
10
$83.25
false

Templates for the grid parts

Each grid part has a render prop with a typed context:

Render prop Context
column renderEditor { value, setValue, inputProps, ... } (see Editing)
column renderFilter { column, field, value, setValue, operator, operators, setOperator, apply, clear, editorProps }
column renderFilterCell the same context, for the filter row cell
renderPager { page, pageSize, pageCount, totalRowCount, pageSizeOptions, goToPage, setPageSize, defaultPager }
renderToolbar { items, defaultToolbar, api } (see Toolbar)
renderEmpty { reason: 'noData' | 'noMatch' }
renderDetail { row, rowId, collapse, ... } (see Detail rows)

renderFilter replaces the value editor inside the filter menu: call setValue to change the draft and let the user press OK, or apply(value) to apply at once and close the menu. renderPager replaces the pager; defaultPager is the built-in one, to wrap or decorate. renderEmpty receives noData when there are no rows and noMatch when the search or the filters match nothing.

Filter, pager and empty templates

Open demo

A column renderFilter in the filter menu, a renderPager pager and a renderEmpty message for "no data" and "no match", all defined once at module scope.

Order
Customer
Product
Category
Amount
ORD-10000
Dewi Lestari
Filter beans
Coffee
$154.00
ORD-10001
Farah Aziz
Oolong tea
Tea
$25.50
ORD-10002
Farah Aziz
Oolong tea
Tea
$145.35
ORD-10003
Dewi Lestari
Filter beans
Coffee
$133.00
ORD-10004
Dewi Lestari
Filter beans
Coffee
$11.90
ORD-10005
Chen Wei
Pour-over kit
Equipment
$159.60
ORD-10006
Jun Park
Oolong tea
Tea
$32.51
ORD-10007
Elif Kaya
Grinder
Equipment
$1,102.95
ORD-10008
Jun Park
Filter beans
Coffee
$146.30
ORD-10009
Goran Petrov
Grinder
Equipment
$1,419.00

Rules for templates

Templates render inside the grid's virtualized rows, so they follow a few rules:

  • Define templates once, at module scope or in a memo, not inline in a component that re-renders often. Render-time functions (renderCell, renderHeader, renderFilter, renderFilterCell, cellProps, cellClassName, headerProps, colSpan, rowSpan) never recompile columns, but other function fields (valueGetter, valueFormatter, sortComparator, ...) compare by identity and recompile the column when they change. Data Grid-level templates (renderRow, renderEmpty, getRowProps, rowClassName, getCellProps) re-render every visible cell when their identity changes. Development builds report a template whose identity changes on three consecutive commits through onWarning (code template-identity-unstable).
  • Read everything from the context. Do not put scroll positions, pointer coordinates, drag state or other high-frequency values into React state for a template to read: the grid keeps those transients outside React, and a state update per scroll frame would re-render every visible cell.
  • Keep templates pure: no side effects while rendering, and no module-level mutable state, so they stay SSR- and React Compiler-safe. Effects inside a template component run when the row mounts and unmount when it scrolls out of the rendered window.

API reference

DataGridColumn lists the column render props and attribute hooks, and DataGridProps the grid ones; the contexts are DataGridCellContext, DataGridHeaderContext, DataGridRowContext, DataGridFilterContext, DataGridPagerContext and DataGridEmptyContext. Per-row and per-cell attributes (getRowProps, getCellProps) are covered in Events.