---
title: "Row Spanning"
framework: angular
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 { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  CellSpanModule,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  ColumnApiModule,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  CellSpanModule,
  ClientSideRowModelModule,
  ColumnApiModule,
]);
import { IOlympicData } from "./interfaces";

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<ag-grid-angular
    style="width: 100%; height: 100%;"
    [columnDefs]="columnDefs"
    [defaultColDef]="defaultColDef"
    [enableCellSpan]="true"
    [rowData]="rowData"
    (gridReady)="onGridReady($event)"
  /> `,
})
export class AppComponent {
  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" },
  ];
  defaultColDef: ColDef = {
    flex: 1,
  };
  rowData!: IOlympicData[];

  constructor(private http: HttpClient) {}

  onGridReady(params: GridReadyEvent<IOlympicData>) {
    this.http
      .get<
        IOlympicData[]
      >("https://www.ag-grid.com/example-assets/olympic-winners.json")
      .subscribe((data) => (this.rowData = data));
  }
}
```

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

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

```ts
<ag-grid-angular
    [columnDefs]="columnDefs"
    [enableCellSpan]="enableCellSpan"
    /* other grid options ... */ />

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

## 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 { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  CellSpanModule,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  SpanRowsParams,
  enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([CellSpanModule, ClientSideRowModelModule]);
import { IOlympicData } from "./interfaces";

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<ag-grid-angular
    style="width: 100%; height: 100%;"
    [columnDefs]="columnDefs"
    [defaultColDef]="defaultColDef"
    [enableCellSpan]="true"
    [rowData]="rowData"
    (gridReady)="onGridReady($event)"
  /> `,
})
export class AppComponent {
  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" },
  ];
  defaultColDef: ColDef = {
    flex: 1,
  };
  rowData!: IOlympicData[];

  constructor(private http: HttpClient) {}

  onGridReady(params: GridReadyEvent<IOlympicData>) {
    this.http
      .get<
        IOlympicData[]
      >("https://www.ag-grid.com/example-assets/olympic-winners.json")
      .subscribe((data) => (this.rowData = data));
  }
}

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

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

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

```ts
<ag-grid-angular
    [columnDefs]="columnDefs"
    [enableCellSpan]="enableCellSpan"
    /* other grid options ... */ />

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

## 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 { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  CellSpanModule,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  RowAutoHeightModule,
  enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  CellSpanModule,
  ClientSideRowModelModule,
  RowAutoHeightModule,
]);
import { IOlympicData } from "./interfaces";

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<ag-grid-angular
    style="width: 100%; height: 100%;"
    [columnDefs]="columnDefs"
    [defaultColDef]="defaultColDef"
    [enableCellSpan]="true"
    [rowData]="rowData"
    (gridReady)="onGridReady($event)"
  /> `,
})
export class AppComponent {
  columnDefs: ColDef[] = [
    {
      field: "lorem",
      spanRows: true,
      wrapText: true,
      autoHeight: true,
      minWidth: 300,
    },
    { field: "athlete" },
    { field: "age" },
    { field: "total" },
  ];
  defaultColDef: ColDef = {
    flex: 1,
  };
  rowData!: IOlympicData[];

  constructor(private http: HttpClient) {}

  onGridReady(params: GridReadyEvent<IOlympicData>) {
    this.http
      .get<
        IOlympicData[]
      >("https://www.ag-grid.com/example-assets/olympic-winners.json")
      .subscribe((data) => {
        data.forEach((row, i) => {
          if (i % 3 === 0) {
            return;
          }
          row.lorem = lorem;
        });
        this.rowData = data;
      });
  }
}

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

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

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

```ts
<ag-grid-angular
    [columnDefs]="columnDefs"
    [enableCellSpan]="enableCellSpan"
    /* other grid options ... */ />

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