Data Grouping in React DataTable

React DataTable: Grouping

Data grouping in React DataTable.

A live React example of jqxDataTable, the jQWidgets DataTable component, with its complete source code below. It is one of 44 jqxDataTable demos for React.

jqxDataTable API used in this demo

Source code

To run it in your own app, install the package with npm install jqwidgets-scripts, add these files to a Vite React and TypeScript project, and import jqx.base.css and a theme in main.tsx. Getting started with jqxDataTable shows the setup step by step.

App.tsx

import { useMemo, useCallback } from 'react';
import JqxDataTable, { jqx } from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxdatatable';

interface DataTableSourceField {
    name: string;
    type: 'string' | 'number' | 'date';
}

interface DataTableSource {
    dataFields: DataTableSourceField[];
    dataType: 'xml';
    record: string;
    root: string;
    url: string;
}

interface Column {
    text: string;
    hidden?: boolean;
    cellsAlign?: 'left' | 'right' | 'center';
    align?: 'left' | 'right' | 'center';
    dataField: string;
    width?: number;
    cellsFormat?: string;
}

function App() {
    const source: DataTableSource = useMemo(() => ({
        dataFields: [
            { name: 'SupplierName', type: 'string' },
            { name: 'Quantity', type: 'number' },
            { name: 'OrderDate', type: 'date' },
            { name: 'OrderAddress', type: 'string' },
            { name: 'Freight', type: 'number' },
            { name: 'Price', type: 'number' },
            { name: 'City', type: 'string' },
            { name: 'ProductName', type: 'string' },
            { name: 'Address', type: 'string' }
        ],
        dataType: 'xml',
        record: 'ROW',
        root: 'DATA',
        url: 'orderdetailsextended.xml'
    }), []);

    const columns: Column[] = useMemo(() => [
        { text: 'Supplier Name', hidden: true, cellsAlign: 'left', align: 'left', dataField: 'SupplierName', width: 280 },
        { text: 'Product Name', cellsAlign: 'left', align: 'left', dataField: 'ProductName', width: 250 },
        { text: 'Quantity', dataField: 'Quantity', cellsFormat: 'd', cellsAlign: 'right', align: 'right', width: 80 },
        { text: 'Price', dataField: 'Price', cellsFormat: 'c2', align: 'right', cellsAlign: 'right', width: 70 },
        { text: 'Address', cellsAlign: 'center', align: 'center', dataField: 'Address', width: 250 },
        { text: 'City', cellsAlign: 'center', align: 'center', dataField: 'City' }
    ], []);

    const groups: string[] = useMemo(() => ['SupplierName'], []);

    const groupsRenderer = useCallback((value: string, _rowData: unknown, _level: number): string => {
        return 'Supplier Name: ' + value;
    }, []);

    const dataAdapter = useMemo(() => new jqx.dataAdapter(source), [source]);

    return (
        <JqxDataTable
            theme="tailwind-blue-dark"
            width="100%"
            source={dataAdapter}
            columns={columns}
            altRows={true}
            pageable={true}
            sortable={true}
            groups={groups}
            groupsRenderer={groupsRenderer}
        />
    );
}

export default App;

Learn more

More React DataTable demos