React Data GridRow Spanning

A single cell can be used to represent multiple contiguous leaf rows with equal values.

Enabling Row Spanning

The example above demonstrates merging cells with equal values into a single cell that spans multiple rows.

The following snippet demonstrates enabling row spanning by setting gridOption.enableCellSpan to true. The country, year, and sport columns then configure row span by setting colDef.spanRows to true.

const [columnDefs, setColumnDefs] = useState([
    {
        field: 'country',
        spanRows: true,
    },
    {
        field: 'year',
        spanRows: true,
    },
    {
        field: 'sport',
        spanRows: true,
    },
    // other column definitions ...
]);
const enableCellSpan = true;

<AgGridReact
    columnDefs={columnDefs}
    enableCellSpan={enableCellSpan}
/>

Custom Row Spanning

Row spanning can be customized by providing a callback function to colDef.spanRows which returns true if the two rows should be spanned together.

The example below demonstrates custom row spanning which prevents any country cells with the value "Algeria" from being spanned.

The following snippet demonstrates how to configure custom row spanning on the country column:

const [columnDefs, setColumnDefs] = useState([
    {
        field: 'country',
        spanRows: ({ valueA, valueB }) => valueA != 'Algeria' && valueA === valueB,
    },
    // other column definitions ...
]);
const enableCellSpan = true;

<AgGridReact
    columnDefs={columnDefs}
    enableCellSpan={enableCellSpan}
/>

Auto Height and Row Spanning

Row spanning can be configured alongside auto height. Note when doing so, if the cell is taller than the combined height of the rows, the last row in the span gains any additional required height.

The following snippet demonstrates how to configure auto height and row spanning:

const [columnDefs, setColumnDefs] = useState([
    {
        field: 'lorem',
        spanRows: true,
        autoHeight: true,
        wrapText: true,
    },
    // other column definitions ...
]);
const enableCellSpan = true;

<AgGridReact
    columnDefs={columnDefs}
    enableCellSpan={enableCellSpan}
/>