---
title: "Tree Data - Tree Selection"
enterprise: true
framework: javascript
version: "36.1.0"
---

# Tree Data - Tree Selection

Row Selection can allow users to select rows in a tree structure.

## Selecting Descendants

When using [Multiple Row Selection](https://www.ag-grid.com/javascript-data-grid/row-selection-multi-row/) with a tree structure, the grid can be configured to impact descendant and ancestor rows when a row is selected.

To enable hierarchical selection, set the `rowSelection.groupSelects` option to one of the following values:

- `'self'` (default): Selecting a row selects only the row itself.
- `'descendants'`: Selecting a row will select all of its descendants. Its ancestor row will become indeterminate, unless all of its descendant rows are selected.
- `'filteredDescendants'`: Selecting a group row will select all of its descendants that pass the filter. Its ancestor row will become indeterminate, unless all of its descendant rows are selected.

#### Group Selection

```ts
import {
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  GroupSelectionMode,
  ModuleRegistry,
  QuickFilterModule,
  RowSelectionModule,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnMenuModule,
  ColumnsToolPanelModule,
  ContextMenuModule,
  TreeDataModule,
} from "ag-grid-enterprise";
import { getData } from "./data";

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

ModuleRegistry.registerModules([
  RowSelectionModule,
  QuickFilterModule,
  ClientSideRowModelModule,
  ColumnsToolPanelModule,
  ColumnMenuModule,
  ContextMenuModule,
  TreeDataModule,
]);

let gridApi: GridApi;

const gridOptions: GridOptions = {
  columnDefs: [
    { 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`;
        }
      },
    },
  ],
  defaultColDef: {
    flex: 1,
    minWidth: 100,
  },
  autoGroupColumnDef: {
    headerName: "File Explorer",
    minWidth: 280,
    cellRenderer: "agGroupCellRenderer",
    cellRendererParams: {
      suppressCount: true,
    },
  },
  rowSelection: {
    mode: "multiRow",
    groupSelects: "self",
  },
  groupDefaultExpanded: -1,
  suppressAggFuncInHeader: true,
  rowData: getData(),
  treeData: true,
  getDataPath: (data) => data.path,
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

function getGroupSelectsValue(): GroupSelectionMode {
  return (
    (document.querySelector<HTMLSelectElement>("#input-group-selection-mode")
      ?.value as any) ?? "self"
  );
}

function onSelectionModeChange() {
  gridApi.setGridOption("rowSelection", {
    mode: "multiRow",
    groupSelects: getGroupSelectsValue(),
  });
}

function onQuickFilterChanged() {
  gridApi.setGridOption(
    "quickFilterText",
    document.querySelector<HTMLInputElement>("#input-quick-filter")?.value,
  );
}

if (typeof window !== "undefined") {
  // Attach external event handlers to window so they can be called from index.html
  (<any>window).onSelectionModeChange = onSelectionModeChange;
  (<any>window).onQuickFilterChanged = onQuickFilterChanged;
}
```

[Live example: Group Selection](https://www.ag-grid.com/examples/tree-data-selection/group-selection/typescript)

## Checkboxes in Group Cells

When using [Row Selection](https://www.ag-grid.com/javascript-data-grid/row-selection/) with Tree Data, the grid can be configured to render checkboxes in the group cell, to the right of the expand/collapse chevron.

This can be configured by setting the `rowSelection.checkboxLocation` to `'autoGroupColumn'`.

#### Group Cell Checkboxes

```ts
import {
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  RowSelectionModule,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnMenuModule,
  ColumnsToolPanelModule,
  ContextMenuModule,
  TreeDataModule,
} from "ag-grid-enterprise";
import { getData } from "./data";

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

ModuleRegistry.registerModules([
  RowSelectionModule,
  ClientSideRowModelModule,
  ColumnsToolPanelModule,
  ColumnMenuModule,
  ContextMenuModule,
  TreeDataModule,
]);

let gridApi: GridApi;

const gridOptions: GridOptions = {
  columnDefs: [
    { 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`;
        }
      },
    },
  ],
  defaultColDef: {
    flex: 1,
    minWidth: 100,
  },
  autoGroupColumnDef: {
    headerName: "File Explorer",
    minWidth: 280,
    cellRenderer: "agGroupCellRenderer",
    cellRendererParams: {
      suppressCount: true,
    },
  },
  rowData: getData(),
  treeData: true,
  getDataPath: (data) => data.path,
  rowSelection: {
    mode: "multiRow",
    checkboxLocation: "autoGroupColumn",
    headerCheckbox: false,
  },
  groupDefaultExpanded: -1,
  suppressAggFuncInHeader: true,
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
```

[Live example: Group Cell Checkboxes](https://www.ag-grid.com/examples/tree-data-selection/group-cell-checkboxes/typescript)

The example above demonstrates the following configuration to render checkboxes in the group cell:

```js
const gridOptions = {
    rowSelection: {
        mode: 'multiRow',
        checkboxLocation: 'autoGroupColumn',
        headerCheckbox: false,
    },

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