Render Components in Grid Cells

Render Components in Grid Cells

Smart.Grid: Rendering React Components in Grid Cells

This article explains how to integrate custom React components inside cells of the Smart.Grid component, enabling dynamic, interactive data grids with React functionality.

Overview

Smart.Grid is a powerful data grid component that helps manage large datasets with a flexible layout. By integrating React components within grid cells, you can enhance user interactivity and customize content rendering based on your application's needs.

In this article, we'll demonstrate how to render React components inside Smart.Grid cells using React, providing an example of rendering a custom cell with dynamic styling and content.

Prerequisites

Before proceeding, ensure that the following prerequisites are met:

  • A basic understanding of React and JSX syntax.
  • Smart.WebComponents and React setup in your project. If you're using create-react-app or another setup, ensure it is properly configured.
  • Basic knowledge of Smart.Grid API and usage.

Rendering React Components in Grid Cells

The following example demonstrates how to render a custom React component inside a Smart.Grid cell. We'll create a custom cell that displays a product name with special styling and an emoji icon.

Example Code

import 'smart-webcomponents-react/source/styles/smart.default.css';
import React, { useState, useRef, useEffect } from "react";
import ReactDOM from 'react-dom/client';
import { Smart, Grid } from 'smart-webcomponents-react/grid';

const MyCustomCell = ({ value }) => {
    return (
        <div style={{ color: 'blue', fontWeight: 'bold' }}>
            📦 {value}
        </div>
    );
};

function App() {

    const behavior = {
        columnResizeMode: 'growAndShrink'
    }

    const appearance = {
        alternationCount: 2,
        showRowHeader: true,
        showRowHeaderSelectIcon: true,
        showRowHeaderFocusIcon: true
    }

    const paging = {
        enabled: true
    }

    const pager = {
        visible: true
    }

    const sorting = {
        enabled: true
    }

    const editing = {
        enabled: true
    }

    const selection = {
        enabled: true,
        allowCellSelection: true,
        allowRowHeaderSelection: true,
        allowColumnHeaderSelection: true,
        mode: 'extended'
    }
    
    const dataSourceSettings = {
        dataFields: [
            'firstName: string',
            'lastName: string',
            'productName: string'
        ]
    }
    
    const dataSource = [  
        { "firstName": "Beate", "lastName": "Wilson", "productName": "Caramel Latte"},   
        { "firstName": "Ian", "lastName": "Nodier", "productName": "Caramel Latte"},   
        { "firstName": "Petra", "lastName": "Vileid", "productName": "Green Tea"},   
        { "firstName": "Mayumi", "lastName": "Ohno", "productName": "Caramel Latte"},   
        { "firstName": "Mayumi", "lastName": "Saylor", "productName": "Espresso con Panna"},   
        { "firstName": "Regina", "lastName": "Fuller", "productName": "Caffe Americano" },  
        { "firstName": "Regina", "lastName": "Burke", "productName": "Caramel Latte"},   
        { "firstName": "Andrew", "lastName": "Petersen", "productName": "Caffe Americano"},  
        { "firstName": "Martin", "lastName": "Ohno", "productName": "Espresso con Panna"},   
        { "firstName": "Beate", "lastName": "Devling", "productName": "Green Tea"},   
        { "firstName": "Sven", "lastName": "Devling", "productName": "Espresso Truffle"},  
        { "firstName": "Petra", "lastName": "Burke", "productName": "Peppermint Mocha Twist"},  
        { "firstName": "Marco", "lastName": "Johnes", "productName": "Caffe Mocha"}  
    ]

    const columns = [{
        label: 'First Name',
        dataField: 'firstName'
    },
    {
        label: 'Last Name',
        dataField: 'lastName',
        template: MyCustomCell
    },
    {
        label: 'Product',
        dataField: 'productName'
    }
    ]

    return (
        <div>
            <Grid
                dataSourceSettings={dataSourceSettings}
                dataSource={dataSource}
                columns={columns}
                appearance={appearance}
                behavior={behavior}
                selection={selection}
                paging={paging}
                pager={pager}
                sorting={sorting}
                editing={editing}
            >
            </Grid>
        </div>
    );
}

export default App;
        

Explanation

In the code above, we are rendering a custom React component called MyCustomCell in the last name column of the grid. This component is used to display the value of the lastName field with custom styling (blue color and bold text) and an emoji.

We define the MyCustomCell component to accept the cell value as a prop and render it with custom styles. The columns configuration in the Smart.Grid component specifies that the template for the "Last Name" column is the MyCustomCell component. The grid automatically renders this component for each row's last name.

Key Concepts

  • Smart.Grid Component: A flexible data grid that allows you to display tabular data with various features such as sorting, paging, editing, and more.
  • React Component in Grid Cell: You can customize the content of each grid cell by rendering React components inside them. This enhances the interactivity of your data grid.
  • Column Templates: The template property in the column configuration allows you to specify a React component for rendering custom content in each cell.

Conclusion

By following the steps outlined in this guide, you can easily integrate React components within your Smart.Grid cells, enabling you to build dynamic, interactive, and highly customizable data grids. This approach enhances the flexibility of your grid, allowing you to add unique elements such as interactive UI components, data visualizations, and custom styles.