---
title: "Tree Data - Expanding Groups"
enterprise: true
framework: vue
version: "36.1.0"
---

# Tree Data - 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 {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import {
  AutoGroupColumnDef,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GetDataPath,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";
import { TreeDataModule } from "ag-grid-enterprise";
import { getData } from "./data";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  TreeDataModule,
  TextFilterModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <ag-grid-vue
      style="width: 100%; height: 100%;"
      @grid-ready="onGridReady"
      :columnDefs="columnDefs"
      :defaultColDef="defaultColDef"
      :autoGroupColumnDef="autoGroupColumnDef"
      :groupDefaultExpanded="groupDefaultExpanded"
      :treeData="true"
      :getDataPath="getDataPath"
      :rowData="rowData"></ag-grid-vue>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "created" },
      { field: "modified" },
      {
        field: "size",
        aggFunc: "sum",
        valueFormatter: (params) => {
          const sizeInKb = params.value / 1024;
          if (sizeInKb > 1024) {
            return `${+(sizeInKb / 1024).toFixed(2)} MB`;
          } else {
            return `${+sizeInKb.toFixed(2)} KB`;
          }
        },
      },
    ]);
    const defaultColDef = ref<ColDef>({
      flex: 1,
      minWidth: 100,
    });
    const autoGroupColumnDef = ref<AutoGroupColumnDef>({
      headerName: "File Explorer",
      minWidth: 280,
      filter: "agTextColumnFilter",
      cellRendererParams: {
        suppressCount: true,
      },
    });
    const groupDefaultExpanded = ref(1);
    const getDataPath = ref<GetDataPath>((data) => data.path);
    const rowData = ref<any[] | null>(getData());

    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      autoGroupColumnDef,
      groupDefaultExpanded,
      getDataPath,
      rowData,
      onGridReady,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

[Live example: Group Default Expanded](https://www.ag-grid.com/examples/tree-data-opening-groups/group-default-expanded/vue3)

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

```ts
<ag-grid-vue
    :groupDefaultExpanded="groupDefaultExpanded"
    /* other grid options ... */>
</ag-grid-vue>

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/vue-data-grid/modules/), [`TreeDataModule`](https://www.ag-grid.com/vue-data-grid/modules/). |

#### Open by Default

```ts
import {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import {
  AutoGroupColumnDef,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GetDataPath,
  GridApi,
  GridOptions,
  GridReadyEvent,
  IsGroupOpenByDefault,
  IsGroupOpenByDefaultParams,
  ModuleRegistry,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";
import { TreeDataModule } from "ag-grid-enterprise";
import { getData } from "./data";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  TreeDataModule,
  TextFilterModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <ag-grid-vue
      style="width: 100%; height: 100%;"
      @grid-ready="onGridReady"
      :columnDefs="columnDefs"
      :defaultColDef="defaultColDef"
      :autoGroupColumnDef="autoGroupColumnDef"
      :isGroupOpenByDefault="isGroupOpenByDefault"
      :treeData="true"
      :getDataPath="getDataPath"
      :rowData="rowData"></ag-grid-vue>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "created" },
      { field: "modified" },
      {
        field: "size",
        aggFunc: "sum",
        valueFormatter: (params) => {
          const sizeInKb = params.value / 1024;
          if (sizeInKb > 1024) {
            return `${+(sizeInKb / 1024).toFixed(2)} MB`;
          } else {
            return `${+sizeInKb.toFixed(2)} KB`;
          }
        },
      },
    ]);
    const defaultColDef = ref<ColDef>({
      flex: 1,
      minWidth: 100,
      filter: true,
    });
    const autoGroupColumnDef = ref<AutoGroupColumnDef>({
      headerName: "File Explorer",
      minWidth: 280,
      filter: "agTextColumnFilter",
      cellRendererParams: {
        suppressCount: true,
      },
    });
    const isGroupOpenByDefault = ref<IsGroupOpenByDefault>(
      (params: IsGroupOpenByDefaultParams) => {
        return (
          (params.level === 0 && params.key === "Documents") ||
          (params.level === 1 && params.key === "Work") ||
          (params.level === 2 && params.key === "ProjectBeta")
        );
      },
    );
    const getDataPath = ref<GetDataPath>((data) => data.path);
    const rowData = ref<any[] | null>(getData());

    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      autoGroupColumnDef,
      isGroupOpenByDefault,
      getDataPath,
      rowData,
      onGridReady,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

[Live example: Open by Default](https://www.ag-grid.com/examples/tree-data-opening-groups/open-by-default/vue3)

The example above uses the following configuration to expand the `ProjectBeta` groups by default:

```ts
<ag-grid-vue
    :isGroupOpenByDefault="isGroupOpenByDefault"
    /* other grid options ... */>
</ag-grid-vue>

this.isGroupOpenByDefault = (params) => {
    return (
        (params.level === 0 && params.key === 'Documents') ||
        (params.level === 1 && params.key === 'Work') ||
        (params.level === 2 && params.key === 'ProjectBeta')
    );
};
```

> **Note**
>
> Row keys are not always unique, so it is recommended to instead use the node ID or data path to identify the row.

## 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 {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import {
  AutoGroupColumnDef,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GetDataPath,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  RowGroupOpenedEvent,
  ScrollApiModule,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";
import { TreeDataModule } from "ag-grid-enterprise";
import { getData } from "./data";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ScrollApiModule,
  ClientSideRowModelModule,
  TreeDataModule,
  TextFilterModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <ag-grid-vue
      style="width: 100%; height: 100%;"
      @grid-ready="onGridReady"
      :columnDefs="columnDefs"
      :defaultColDef="defaultColDef"
      :autoGroupColumnDef="autoGroupColumnDef"
      :rowData="rowData"
      :treeData="true"
      :getDataPath="getDataPath"
      :animateRows="false"
      @row-group-opened="onRowGroupOpened"></ag-grid-vue>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "created" },
      { field: "modified" },
      {
        field: "size",
        aggFunc: "sum",
        valueFormatter: (params) => {
          const sizeInKb = params.value / 1024;
          if (sizeInKb > 1024) {
            return `${+(sizeInKb / 1024).toFixed(2)} MB`;
          } else {
            return `${+sizeInKb.toFixed(2)} KB`;
          }
        },
      },
    ]);
    const defaultColDef = ref<ColDef>({
      flex: 1,
      minWidth: 100,
      filter: true,
    });
    const autoGroupColumnDef = ref<AutoGroupColumnDef>({
      headerName: "File Explorer",
      minWidth: 280,
      filter: "agTextColumnFilter",
      cellRendererParams: {
        suppressCount: true,
      },
    });
    const rowData = ref<any[] | null>(getData());
    const getDataPath = ref<GetDataPath>((data) => data.path);

    function 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;
        gridApi.value!.ensureIndexVisible(newIndex);
      }
    }
    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      autoGroupColumnDef,
      rowData,
      getDataPath,
      onGridReady,
      onRowGroupOpened,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

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

## API

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/vue-data-grid/modules/), [`ServerSideRowModelApiModule`](https://www.ag-grid.com/vue-data-grid/modules/). |
| `collapseAll` | `Function` |  |  | Collapse all groups. Modules (any of): [`ClientSideRowModelApiModule`](https://www.ag-grid.com/vue-data-grid/modules/), [`ServerSideRowModelApiModule`](https://www.ag-grid.com/vue-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/vue-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 {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import {
  AutoGroupColumnDef,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GetDataPath,
  GetRowIdFunc,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  RowApiModule,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";
import { TreeDataModule } from "ag-grid-enterprise";
import { getData } from "./data";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  RowApiModule,
  ClientSideRowModelModule,
  TreeDataModule,
  TextFilterModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <ag-grid-vue
      style="width: 100%; height: 100%;"
      @grid-ready="onGridReady"
      :columnDefs="columnDefs"
      :defaultColDef="defaultColDef"
      :autoGroupColumnDef="autoGroupColumnDef"
      :rowData="rowData"
      :treeData="true"
      :getDataPath="getDataPath"
      :getRowId="getRowId"></ag-grid-vue>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "created" },
      { field: "modified" },
      {
        field: "size",
        aggFunc: "sum",
        valueFormatter: (params) => {
          const sizeInKb = params.value / 1024;
          if (sizeInKb > 1024) {
            return `${+(sizeInKb / 1024).toFixed(2)} MB`;
          } else {
            return `${+sizeInKb.toFixed(2)} KB`;
          }
        },
      },
    ]);
    const defaultColDef = ref<ColDef>({
      flex: 1,
      minWidth: 100,
      filter: true,
    });
    const autoGroupColumnDef = ref<AutoGroupColumnDef>({
      headerName: "File Explorer",
      minWidth: 280,
      filter: "agTextColumnFilter",
      cellRendererParams: {
        suppressCount: true,
      },
    });
    const rowData = ref<any[] | null>(getData());
    const getDataPath = ref<GetDataPath>((data) => data.path);
    const getRowId = ref<GetRowIdFunc>(
      (params) => params.data.path[params.data.path.length - 1],
    );

    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;

      const node = params.api.getRowNode("Proposal.docx");
      if (node) {
        params.api.setRowNodeExpanded(node, true, true);
      }
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      autoGroupColumnDef,
      rowData,
      getDataPath,
      getRowId,
      onGridReady,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

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

The example above uses [Row IDs](https://www.ag-grid.com/vue-data-grid/row-ids/#row-ids) to demonstrate the following configuration to expand all of the 'Proposal.docx' row's ancestors:

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