Column Templates in React Data Grid

The Syncfusion® React Data Grid component enables customization of cell content through the template property in the Column component. This feature supports rich, interactive displays, such as styled statuses, images, or buttons, beyond plain text. This section explores various template scenarios, best practices, and advanced use cases to enhance grid functionality.

Basic template

The template property in the Data Grid component allows customization of each cell using a function that returns JSX. This function receives the current row data and column details as props, enabling dynamic content rendering based on specific values.

Loading...

The column template custom function receives a parameter of type ColumnTemplateProps, an interface providing access to the current row data and column configuration for rendering.

Row data with templates

In Data Grid component, templates receive the current row data object as props, providing access to all fields and metadata like row index. This enables the creation of complex cell content, such as combining multiple data points or applying styling based on the row position.

Loading...

Conditional templates

Conditional templates enable rendering different cell content based on specific values or conditions, enhancing readability and visual distinction of data.

Loading...

Button template

Button templates in the Data Grid component add interactivity by placing buttons within grid cells.

In the example below, a view button opens a Dialog component to show detailed information about a restaurant item, including its name, category, price, description, and spice level. Clicking the place order button in the dialog displays a Message component, confirming that the order was successfully placed.

Loading...

Image template

Image templates allow displaying visuals, such as employee photos or product thumbnails, directly in grid cells.

Loading...

Hyperlink templates in the grid make cells interactive by rendering values as clickable links, useful for linking to websites, email addresses, or other external resources.

In the example below, the "First Name" column is rendered as a hyperlink using the template property with an <a> tag.

Loading...

Component integration in templates

Templates support both Syncfusion® and custom React components, allowing rich visualizations and interactive layouts.

In the below example, a Chart component can be rendered inside a cell to show product sales details, enhancing the individuals experience with visual insights.

Loading...

Unique IDs for the Chart component prevent rendering conflicts in grid cell.

Performance optimization for templates

Complex templates can impact Data Grid component performance, especially with large datasets. To improve efficiency:

  • Use React.memo or useCallback to memoize template functions and prevent unnecessary re-renders.

  • Avoid heavy inline styles or computations inside templates, use CSS classes or precomputed values instead.

  • Limit the number of templated columns to maintain smooth scrolling and rendering.

Loading...

Dynamic template switching

Dynamic template switching in Data Grid component allows templates to change based on interaction or application state. This enables flexible data presentation depending on context or individual preference.

For example, a "Progress" column can switch between displaying a progress bar and a text percentage by storing the template function in a state variable and updating it dynamically as needed.

Loading...

See also