---
title: "Row Spanning"
framework: javascript
version: "36.1.0"
---

# Row Spanning

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

#### Row Spanning Simple

```ts
import {
  CellSpanModule,
  ClientSideRowModelModule,
  ColDef,
  ColumnApiModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  CellSpanModule,
  ClientSideRowModelModule,
  ColumnApiModule,
]);

const columnDefs: ColDef[] = [
  { field: "country", spanRows: true, sort: "asc" },
  { field: "year", spanRows: true, sort: "asc" },
  { field: "sport", spanRows: true, sort: "asc" },
  { field: "athlete" },
  { field: "age" },
  { field: "total" },
];

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: columnDefs,
  defaultColDef: {
    flex: 1,
  },
  enableCellSpan: true,
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
```

[Live example: Row Spanning Simple](https://www.ag-grid.com/examples/row-spanning/row-spanning-simple/typescript/)

## Enabling Row Spanning

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

Row spanning requires the `CellSpanModule` to be registered. The `enableCellSpan` grid option is an initial property and cannot be changed after the grid is created.

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

```js
const gridOptions = {
    columnDefs: [
        {
            field: 'country',
            spanRows: true,
        },
        {
            field: 'year',
            spanRows: true,
        },
        {
            field: 'sport',
            spanRows: true,
        },
        // other column definitions ...
    ],
    enableCellSpan: true,

    // other grid options ...
}
```

## Custom Row Spanning

Row spanning can be customised by providing a callback function to `colDef.spanRows`. The callback returns `true` if the two adjacent rows should be spanned together.

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

#### Row Spanning Custom

```ts
import {
  CellSpanModule,
  ClientSideRowModelModule,
  ColDef,
  GridApi,
  GridOptions,
  ModuleRegistry,
  SpanRowsParams,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([CellSpanModule, ClientSideRowModelModule]);

const customSpanFunc = ({ valueA, valueB }: SpanRowsParams) => {
  return valueA != "Algeria" && valueA === valueB;
};

const columnDefs: ColDef[] = [
  { field: "country", spanRows: customSpanFunc, sort: "asc" },
  { field: "year", spanRows: true, sort: "asc" },
  { field: "sport", spanRows: true, sort: "asc" },
  { field: "athlete" },
  { field: "age" },
  { field: "total" },
];

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: columnDefs,
  defaultColDef: {
    flex: 1,
  },
  enableCellSpan: true,
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
```

[Live example: Row Spanning Custom](https://www.ag-grid.com/examples/row-spanning/row-spanning-custom/typescript/)

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

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

    // other grid options ...
}
```

## 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.

#### Row Spanning Auto Height

```ts
import {
  CellSpanModule,
  ClientSideRowModelModule,
  ColDef,
  GridApi,
  GridOptions,
  ModuleRegistry,
  RowAutoHeightModule,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  CellSpanModule,
  ClientSideRowModelModule,
  RowAutoHeightModule,
]);

const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.`;

const columnDefs: ColDef[] = [
  {
    field: "lorem",
    spanRows: true,
    wrapText: true,
    autoHeight: true,
    minWidth: 300,
  },
  { field: "athlete" },
  { field: "age" },
  { field: "total" },
];

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: columnDefs,
  defaultColDef: {
    flex: 1,
  },
  enableCellSpan: true,
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
  .then((response) => response.json())
  .then((data: any[]) => {
    data.forEach((row, i) => {
      if (i % 3 === 0) {
        return;
      }
      row.lorem = lorem;
    });
    gridApi!.setGridOption("rowData", data);
  });
```

[Live example: Row Spanning Auto Height](https://www.ag-grid.com/examples/row-spanning/row-spanning-auto-height/typescript/)

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

```js
const gridOptions = {
    columnDefs: [
        {
            field: 'lorem',
            spanRows: true,
            autoHeight: true,
            wrapText: true,
        },
        // other column definitions ...
    ],
    enableCellSpan: true,

    // other grid options ...
}
```
