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

# Tree Data - Filtering

Filtering can be applied to Tree Data to reduce the range of displayed data.

## Filtering

By default, when a group row passes a [Filter](https://www.ag-grid.com/javascript-data-grid/filtering-overview/), the children will also be displayed.

The example below applies a filter to the group column for 'ProjectAlpha', note that two 'ProjectAlpha' groups are displayed alongside all of their children.

#### Filtering

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

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

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

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,
  },
  autoGroupColumnDef: {
    headerName: "File Explorer",
    minWidth: 150,
    filter: "agTextColumnFilter",

    cellRendererParams: {
      suppressCount: true,
    },
  },
  rowData: getData(),
  treeData: true,
  groupDefaultExpanded: -1,
  getDataPath: (data) => data.path,
  onGridReady: (event) => {
    gridApi.setFilterModel({
      "ag-Grid-AutoColumn": {
        filterType: "text",
        type: "startsWith",
        filter: "ProjectAlpha",
      },
    });
  },
};

// wait for the document to be loaded, otherwise
// AG Grid will not find the div in the document.
// lookup the container we want the Grid to use
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;

// create the grid passing in the div to use together with the columns & data we want to use
gridApi = createGrid(gridDiv, gridOptions);
```

[Live example: Filtering](https://www.ag-grid.com/examples/tree-data-filtering/filtering-simple/typescript/)

## Exclude Children when Filtering

To omit children when a group row passes a filter, set `excludeChildrenWhenTreeDataFiltering` to `true` in the grid options.

The example below applies a default filter to the group column for 'ProjectAlpha', note that only one 'ProjectAlpha' group passes the filter instead of two - this is because the `Desktop -> ProjectAlpha` group is a [Filler Group](https://www.ag-grid.com/javascript-data-grid/tree-data-paths/#filler-groups).

#### Exclude Children when Filtering

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

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

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

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,
  },
  autoGroupColumnDef: {
    headerName: "File Explorer",
    minWidth: 150,
    filter: "agTextColumnFilter",

    cellRendererParams: {
      suppressCount: true,
    },
  },
  rowData: getData(),
  treeData: true,
  groupDefaultExpanded: -1,
  excludeChildrenWhenTreeDataFiltering: true,
  getDataPath: (data) => data.path,
  onGridReady: (event) => {
    gridApi.setFilterModel({
      "ag-Grid-AutoColumn": {
        filterType: "text",
        type: "startsWith",
        filter: "ProjectAlpha",
      },
    });
  },
};

function toggleFilter() {
  const checkbox = document.querySelector<HTMLInputElement>(
    "#excludeChildrenWhenTreeDataFiltering",
  )!;
  gridApi.setGridOption(
    "excludeChildrenWhenTreeDataFiltering",
    checkbox.checked,
  );
}

// wait for the document to be loaded, otherwise
// AG Grid will not find the div in the document.
// lookup the container we want the Grid to use
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;

// create the grid passing in the div to use together with the columns & data we want to use
gridApi = createGrid(gridDiv, gridOptions);

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

[Live example: Exclude Children when Filtering](https://www.ag-grid.com/examples/tree-data-filtering/filtering-exclude-children/typescript/)

This demonstrates the following configuration:

```js
const gridOptions = {
    excludeChildrenWhenTreeDataFiltering: true,

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

## Ignore Filters when Aggregating Values

When using Tree Data and filters, the aggregates are only calculated from the rows which pass the filter. This can be changed by enabling the grid option `suppressAggFilteredOnly`.

The example below has a filter applied resulting in only one of the `Documents` children being displayed. Note that when the `suppressAggFilteredOnly` option is toggled on, the `Documents` group aggregation will display the sum of all children, regardless of the filter.

#### Aggregated Values Based on Pre-Filtered Data

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

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

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

let gridApi: GridApi;

const gridOptions: GridOptions = {
  columnDefs: [
    { field: "created" },
    { field: "modified" },
    {
      field: "size",
      aggFunc: "sum",
      filter: "agNumberColumnFilter",
      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,
  },
  autoGroupColumnDef: {
    headerName: "File Explorer",
    minWidth: 150,

    cellRendererParams: {
      suppressCount: true,
    },
  },
  rowData: getData(),
  treeData: true,
  groupDefaultExpanded: 1,
  suppressAggFilteredOnly: true,
  getDataPath: (data) => data.path,
  onGridReady: (event) => {
    gridApi.setFilterModel({
      size: {
        filterType: "number",
        type: "equals",
        filter: 5193728,
      },
    });
  },
};

function toggleCheckbox() {
  const checkbox = document.querySelector<HTMLInputElement>(
    "#suppressAggFilteredOnly",
  )!;
  gridApi.setGridOption("suppressAggFilteredOnly", checkbox.checked);
}

// wait for the document to be loaded, otherwise
// AG Grid will not find the div in the document.
// lookup the container we want the Grid to use
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;

// create the grid passing in the div to use together with the columns & data we want to use
gridApi = createGrid(gridDiv, gridOptions);

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

[Live example: Aggregated Values Based on Pre-Filtered Data](https://www.ag-grid.com/examples/tree-data-filtering/group-agg-filtering/typescript/)

This demonstrates the following configuration:

```js
const gridOptions = {
    suppressAggFilteredOnly: true,

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

## Filter Components

### Tree Filter

The [Tree Filter](https://www.ag-grid.com/javascript-data-grid/filter-set-tree-list/) is a version of the [Set Filter](https://www.ag-grid.com/javascript-data-grid/filter-set/) that is designed to work with hierarchical data by displaying the set filter in a tree structure that matches the data.

The example below demonstrates the Tree Filter on the Group column:

#### Tree List

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

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

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  ColumnsToolPanelModule,
  FiltersToolPanelModule,
  ColumnMenuModule,
  ContextMenuModule,
  SetFilterModule,
  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: 200,
  },
  autoGroupColumnDef: {
    cellRendererParams: {
      suppressCount: true,
    },
    filter: "agSetColumnFilter",
    filterParams: {
      treeList: true,
      keyCreator: (params) => (params.value ? params.value.join("#") : null),
    },
  },
  treeData: true,
  groupDefaultExpanded: -1,
  getDataPath: (data: any) => data.path,
  rowData: getData(),
};

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

[Live example: Tree List](https://www.ag-grid.com/examples/tree-data-filtering/tree-list-filtering/typescript/)

This demonstrates the following configuration to enable the Tree List Filter. Note that a `keyCreator` must be used to convert each path into a unique string:

```js
const gridOptions = {
    autoGroupColumnDef: {
        filter: 'agSetColumnFilter',
        filterParams: {
            treeList: true,
            keyCreator: (params) => (params.value ? params.value.join('#') : null),
        },
    },

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

For further information, refer to the [Tree Filter](https://www.ag-grid.com/javascript-data-grid/filter-set-tree-list/) documentation.

### Set Filter

The [Set Filter](https://www.ag-grid.com/javascript-data-grid/filter-set/) can be used with Tree Data to list all unique values across each level of the group hierarchy.

The example below demonstrates the Set Filter on the Size column.

#### Set List

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

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

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

let gridApi: GridApi;

const gridOptions: GridOptions = {
  columnDefs: [
    { field: "created" },
    { field: "modified" },
    {
      field: "size",
      aggFunc: "sum",
      filter: "agSetColumnFilter",
      filterParams: {
        valueFormatter: (params: ValueFormatterParams) => {
          const sizeInKb = params.value / 1024;

          if (sizeInKb > 1024) {
            return `${+(sizeInKb / 1024).toFixed(2)} MB`;
          } else {
            return `${+sizeInKb.toFixed(2)} KB`;
          }
        },
      },
      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: 200,
  },
  autoGroupColumnDef: {
    cellRendererParams: {
      suppressCount: true,
    },
  },
  treeData: true,
  groupDefaultExpanded: -1,
  groupAggFiltering: true,
  getDataPath: (data: any) => data.path,
  rowData: getData(),
};

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

[Live example: Set List](https://www.ag-grid.com/examples/tree-data-filtering/set-list-filtering/typescript/)

> **Note**
>
> When using the Set Filter with [Aggregations](https://www.ag-grid.com/javascript-data-grid/aggregation/) the set filter options can change when applying filters.
>
> To prevent this it is advised that you [Enable Aggregate Value Filtering](https://www.ag-grid.com/javascript-data-grid/aggregation-filtering/#filtering-for-aggregated-values).
