---
title: "Row Grouping - Expanding Groups"
enterprise: true
framework: angular
version: "36.1.0"
---

# Row Grouping - Expanding Groups

Configure the initial expanded group row state when using Tree Data.

## Expanding by Group Level

When providing a hierarchy, all levels will default to a collapsed state. This can be configured by setting the `groupDefaultExpanded` grid option. Providing a number will expand all groups down to that level, or providing -1 will expand all groups.

#### Group Default Expanded

```ts
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  AutoGroupColumnDef,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  enableDevValidations,
} from "ag-grid-community";
import { RowGroupingModule } from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([ClientSideRowModelModule, RowGroupingModule]);
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"
    [autoGroupColumnDef]="autoGroupColumnDef"
    [groupDefaultExpanded]="groupDefaultExpanded"
    [rowData]="rowData"
    (gridReady)="onGridReady($event)"
  /> `,
})
export class AppComponent {
  columnDefs: ColDef[] = [
    { field: "country", rowGroup: true, hide: true },
    { field: "year", rowGroup: true, hide: true },
    { field: "athlete" },
    { field: "sport" },
    { field: "gold" },
    { field: "silver" },
    { field: "bronze" },
  ];
  defaultColDef: ColDef = {
    flex: 1,
    minWidth: 100,
  };
  autoGroupColumnDef: AutoGroupColumnDef = {
    minWidth: 200,
  };
  groupDefaultExpanded = 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: Group Default Expanded](https://www.ag-grid.com/examples/grouping-opening-groups/group-default-expanded/angular)

The example above uses the following configuration to expand the first level of groups, but no others:

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

this.groupDefaultExpanded = 1;
```

## Expanding via Callback

To granularly determine which groups should be expanded by default, use the `isGroupOpenByDefault` grid callback.

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `isGroupOpenByDefault` | `IsGroupOpenByDefault` |  |  | (Client-side Row Model only) Allows group rows to be open by default. For master rows use `isMasterOpenByDefault`. Modules (any of): [`RowGroupingModule`](https://www.ag-grid.com/angular-data-grid/modules/), [`TreeDataModule`](https://www.ag-grid.com/angular-data-grid/modules/). |

#### Open by Default

```ts
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  AutoGroupColumnDef,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  IsGroupOpenByDefault,
  IsGroupOpenByDefaultParams,
  ModuleRegistry,
  NumberFilterModule,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";
import { RowGroupingModule } from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  TextFilterModule,
  NumberFilterModule,
  ClientSideRowModelModule,
  RowGroupingModule,
]);
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"
    [autoGroupColumnDef]="autoGroupColumnDef"
    [isGroupOpenByDefault]="isGroupOpenByDefault"
    [rowData]="rowData"
    (gridReady)="onGridReady($event)"
  /> `,
})
export class AppComponent {
  columnDefs: ColDef[] = [
    { field: "country", rowGroup: true },
    { field: "year", rowGroup: true },
    { field: "sport" },
    { field: "athlete" },
    { field: "total" },
  ];
  defaultColDef: ColDef = {
    flex: 1,
    minWidth: 100,
    filter: true,
  };
  autoGroupColumnDef: AutoGroupColumnDef = {
    minWidth: 200,
  };
  isGroupOpenByDefault: IsGroupOpenByDefault = (
    params: IsGroupOpenByDefaultParams,
  ) => {
    const route = params.rowNode.getRoute();
    const destPath = ["Australia", "2004"];
    return !!route?.every((item, idx) => destPath[idx] === item);
  };
  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: Open by Default](https://www.ag-grid.com/examples/grouping-opening-groups/open-by-default/angular)

The example above uses the following configuration to expand the `Australia` and its child `2004` groups by default:

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

this.isGroupOpenByDefault = (params) => {
    const route = params.rowNode.getRoute();
    const destPath = ['Australia', '2004'];
    return route.every((item, idx) => destPath[idx] === item);
};
```

> **Note**
>
> Row keys are only unique within their groups, so it is recommended to instead use the entire [Row Route](https://www.ag-grid.com/angular-data-grid/row-object/#reference-serverSide-getRoute) to identify the row.

## Prevent Sticky Groups

When scrolling through an expanded group, the group row will stick to the top of the grid. To prevent this behaviour, set the `suppressGroupRowsSticky` property to `true`.

#### Sticky Groups

```ts
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  AutoGroupColumnDef,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  enableDevValidations,
} from "ag-grid-community";
import { RowGroupingModule } from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([ClientSideRowModelModule, RowGroupingModule]);
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"
    [autoGroupColumnDef]="autoGroupColumnDef"
    [suppressGroupRowsSticky]="true"
    [groupDefaultExpanded]="groupDefaultExpanded"
    [rowData]="rowData"
    (gridReady)="onGridReady($event)"
  /> `,
})
export class AppComponent {
  columnDefs: ColDef[] = [
    { field: "country", rowGroup: true },
    { field: "year", rowGroup: true },
    { field: "sport" },
    { field: "athlete" },
    { field: "total" },
  ];
  defaultColDef: ColDef = {
    flex: 1,
    minWidth: 100,
  };
  autoGroupColumnDef: AutoGroupColumnDef = {
    minWidth: 200,
  };
  groupDefaultExpanded = 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: Sticky Groups](https://www.ag-grid.com/examples/grouping-opening-groups/suppress-sticky-groups/angular)

The example above uses the following configuration to prevent groups from sticking:

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

this.suppressGroupRowsSticky = true;
```

## Scrolling Child Rows into View

When expanding a group the vertical scroll does not change, which can result in the child rows not being visible. You can use the `ensureIndexVisible()` function on the API to ensure the index is visible, scrolling the table if needed.

In the example below, if you expand a group at the bottom, the grid will scroll so that all of the children of the group are visible.

#### Row Group Scroll

```ts
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  NumberEditorModule,
  RowGroupOpenedEvent,
  RowGroupingDisplayType,
  ScrollApiModule,
  TextEditorModule,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";
import { RowGroupingModule } from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ScrollApiModule,
  NumberEditorModule,
  TextEditorModule,
  TextFilterModule,
  ClientSideRowModelModule,
  RowGroupingModule,
]);
import { IOlympicData } from "./interfaces";

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<ag-grid-angular
    style="width: 100%; height: 100%;"
    [columnDefs]="columnDefs"
    [animateRows]="false"
    [groupDisplayType]="groupDisplayType"
    [defaultColDef]="defaultColDef"
    [rowData]="rowData"
    (rowGroupOpened)="onRowGroupOpened($event)"
    (gridReady)="onGridReady($event)"
  /> `,
})
export class AppComponent {
  private gridApi!: GridApi<IOlympicData>;

  columnDefs: ColDef[] = [
    { field: "athlete", width: 150, rowGroupIndex: 0 },
    { field: "age", width: 90, rowGroupIndex: 1 },
    { field: "country", width: 120, rowGroupIndex: 2 },
    { field: "year", width: 90 },
    { field: "date", width: 110, rowGroupIndex: 2 },
  ];
  groupDisplayType: RowGroupingDisplayType = "groupRows";
  defaultColDef: ColDef = {
    editable: true,
    filter: true,
    flex: 1,
    minWidth: 100,
  };
  rowData!: IOlympicData[];

  constructor(private http: HttpClient) {}

  onRowGroupOpened(event: RowGroupOpenedEvent<IOlympicData>) {
    if (event.expanded) {
      const rowNodeIndex = event.node.rowIndex!;
      // factor in child nodes so we can scroll to correct position
      const childCount = event.node.childrenAfterSort
        ? event.node.childrenAfterSort.length
        : 0;
      const newIndex = rowNodeIndex + childCount;
      this.gridApi.ensureIndexVisible(newIndex);
    }
  }

  onGridReady(params: GridReadyEvent<IOlympicData>) {
    this.gridApi = params.api;

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

[Live example: Row Group Scroll](https://www.ag-grid.com/examples/grouping-opening-groups/row-group-scroll/angular)

## API

> **Note**
>
> The row group expansion state can be saved and restored as part of [Grid State](https://www.ag-grid.com/angular-data-grid/grid-state/).

The grid exposes API methods to expand or collapse groups programmatically.

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `expandAll` | `Function` |  |  | Expand all groups. Modules (any of): [`ClientSideRowModelApiModule`](https://www.ag-grid.com/angular-data-grid/modules/), [`ServerSideRowModelApiModule`](https://www.ag-grid.com/angular-data-grid/modules/). |
| `collapseAll` | `Function` |  |  | Collapse all groups. Modules (any of): [`ClientSideRowModelApiModule`](https://www.ag-grid.com/angular-data-grid/modules/), [`ServerSideRowModelApiModule`](https://www.ag-grid.com/angular-data-grid/modules/). |
| `resetRowGroupExpansion` | `Function` |  |  | Reset all group expansion to defaults, as determined by `groupDefaultExpanded`, `isGroupOpenByDefault`, or `isServerSideGroupOpenByDefault`. Any user-initiated expand/collapse overrides are discarded. Modules (any of): [`ClientSideRowModelApiModule`](https://www.ag-grid.com/angular-data-grid/modules/), [`ServerSideRowModelApiModule`](https://www.ag-grid.com/angular-data-grid/modules/). |
| `setRowNodeExpanded` | `Function` |  |  | Expand or collapse a specific row node, optionally expanding/collapsing all of its parent nodes. By default rows are expanded asynchronously for best performance. Set `forceSync: true` if you need to interact with the expanded row immediately after this function. Module: [`RowApiModule`](https://www.ag-grid.com/angular-data-grid/modules/). |

### Expand Row Ancestors

When expanding rows via the API, the `setRowNodeExpanded` function can be used to expand a specific row as well as all of its ancestors.

#### Expand to Row

```ts
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GetRowIdFunc,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  RowApiModule,
  enableDevValidations,
} from "ag-grid-community";
import { RowGroupingModule } from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  RowApiModule,
  ClientSideRowModelModule,
  RowGroupingModule,
]);

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<ag-grid-angular
    style="width: 100%; height: 100%;"
    [columnDefs]="columnDefs"
    [defaultColDef]="defaultColDef"
    [getRowId]="getRowId"
    [rowData]="rowData"
    (firstDataRendered)="onFirstDataRendered($event)"
    (gridReady)="onGridReady($event)"
  /> `,
})
export class AppComponent {
  private gridApi!: GridApi;

  columnDefs: ColDef[] = [
    { field: "country", rowGroup: true, hide: true },
    { field: "year", rowGroup: true, hide: true },
    { field: "athlete" },
    { field: "total" },
  ];
  defaultColDef: ColDef = {
    flex: 1,
    minWidth: 150,
  };
  getRowId: GetRowIdFunc = (params) => params.data.id;
  rowData!: any[];

  constructor(private http: HttpClient) {}

  onFirstDataRendered() {
    const node = this.gridApi.getRowNode("2");
    if (node) {
      this.gridApi.setRowNodeExpanded(node, true, true);
    }
  }

  onGridReady(params: GridReadyEvent) {
    this.gridApi = params.api;

    this.http
      .get<any[]>("https://www.ag-grid.com/example-assets/olympic-winners.json")
      .subscribe(
        (data) =>
          (this.rowData = data.map((d, i) => ({ ...d, id: String(i) }))),
      );
  }
}
```

[Live example: Expand to Row](https://www.ag-grid.com/examples/grouping-opening-groups/expand-collapse-api/angular)

The example above uses [Row IDs](https://www.ag-grid.com/angular-data-grid/row-ids/#row-ids) to demonstrate the following configuration to expand all of the ancestors of the row with the ID `"2"`:

```
const expandToRow = () => {
  const node = gridApi.getRowNode('2');
  if (node) {
      gridApi.setRowNodeExpanded(node, true, true);
  }
}
```

### Reset Group Expansion

After users have expanded or collapsed groups, `resetRowGroupExpansion()` discards all overrides and re-evaluates each group against the configured defaults (`groupDefaultExpanded` or `isGroupOpenByDefault`).

In the example below, the `isGroupOpenByDefault` callback expands the `Australia > 2004` path by default. Try expanding or collapsing groups, then click **Reset to Defaults** to restore the original expansion state.

#### Reset Group Expansion

```ts
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import "./styles.css";
import {
  AutoGroupColumnDef,
  ClientSideRowModelApiModule,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  IsGroupOpenByDefault,
  IsGroupOpenByDefaultParams,
  ModuleRegistry,
  RowApiModule,
  enableDevValidations,
} from "ag-grid-community";
import { RowGroupingModule } from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

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

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<div class="example-wrapper">
    <div style="margin-bottom: 5px">
      <button (click)="onBtExpandAll()">Expand All</button>
      <button (click)="onBtCollapseAll()">Collapse All</button>
      <button (click)="onBtResetExpansion()">Reset to Defaults</button>
    </div>
    <ag-grid-angular
      style="width: 100%; height: 100%;"
      [columnDefs]="columnDefs"
      [defaultColDef]="defaultColDef"
      [autoGroupColumnDef]="autoGroupColumnDef"
      [isGroupOpenByDefault]="isGroupOpenByDefault"
      [rowData]="rowData"
      (gridReady)="onGridReady($event)"
    />
  </div> `,
})
export class AppComponent {
  private gridApi!: GridApi<IOlympicData>;

  columnDefs: ColDef[] = [
    { field: "country", rowGroup: true },
    { field: "year", rowGroup: true },
    { field: "sport" },
    { field: "athlete" },
    { field: "total" },
  ];
  defaultColDef: ColDef = {
    flex: 1,
    minWidth: 100,
  };
  autoGroupColumnDef: AutoGroupColumnDef = {
    minWidth: 200,
  };
  isGroupOpenByDefault: IsGroupOpenByDefault = (
    params: IsGroupOpenByDefaultParams,
  ) => {
    const route = params.rowNode.getRoute();
    const destPath = ["Australia", "2004"];
    return !!route?.every((item, idx) => destPath[idx] === item);
  };
  rowData!: IOlympicData[];

  constructor(private http: HttpClient) {}

  onBtExpandAll() {
    this.gridApi.expandAll();
  }

  onBtCollapseAll() {
    this.gridApi.collapseAll();
  }

  onBtResetExpansion() {
    this.gridApi.resetRowGroupExpansion();
  }

  onGridReady(params: GridReadyEvent<IOlympicData>) {
    this.gridApi = params.api;

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

[Live example: Reset Group Expansion](https://www.ag-grid.com/examples/grouping-opening-groups/reset-group-expansion/angular)
