# Row Grouping - Filtering

Canonical page: https://www.adaptabletools.com/docs/handbook-grouping-rows-filtering

- Row Grouped Columns can be filtered like any other Column
- When there are multiple Row-Grouped Columns, AdapTable provides a Tree-based structure

Row Grouped Columns can be [filtered](https://www.adaptabletools.com/docs/handbook-column-filter/index.md) like any other column. 

- If a Column being Row-Grouped is already filtered, then the filter will apply to the Row Group
- Only the values that currently appear in the column being grouped, will display in the Row Group filter

AdapTable's Row Grouped column filter behaviour differs based on the value of the Layout's `RowGroupDisplayType` property which can be:
- `single`- all Row Group Columns appear in one column
- `multi` - each Row Grouped Column appears in its own Column

## `RowGroupDisplayType`: Single

When the `RowGroupDisplayType` is *single*, AG Grid will display one Column for all Row Grouped Columns.

AdapTable will automatically change the default Filter type to the [In Filter](https://www.adaptabletools.com/docs/handbook-column-filter-in-filter/index.md).

Make sure to set `floatingFilter` to *true* in AG Grid `GridOptions`

The look and feel of the `In` Filter values differs depending on how many Columns are being Row Grouped:
- one Column only - AdapTable will simply display a list of all distinct values in the Row Grouped Column
- 2 (or more) Columns - AdapTable will show the grouped columns as an expandable **Tree** structure (with a button to expand / collapse all "leafs" in the tree)

**Example: Row Grouping: Filtering Single RowGroupDisplayType**

Filtering Row Grouped Columns with Display Type Single

 

- In this example we have provided 2 Layouts with `RowGroupDisplayType` set to *single*:
  - `DisplayType Single 1 Row Group` has just one Row Group (`Language` Column) and so values are displayed in a **list**
  - `DisplayType Single 2 Row Groups` has 2 Row Groups (`Language` and `License` Columns) and so values are displayed in a **tree** structure

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Grouping: Filtering DisplayType Single',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout', 'ColumnFilter']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'DisplayType Single 1 Row Group',
      Layouts: [
        {
          Name: 'DisplayType Single 1 Row Group',
          RowGroupedColumns: ['language'],
          RowGroupDisplayType: 'single',
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          SuppressAggFuncInHeader: true,
          TableColumns: [
            'name',
            'github_stars',
            'github_watchers',
            'license',
            'updated_at',
            'description',
            'created_at',
          ],
          AutoSizeColumns: true,
        },
        {
          Name: 'DisplayType Single 2 Row Groups',
          RowGroupedColumns: ['language', 'license'],
          RowGroupDisplayType: 'single',
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],

          SuppressAggFuncInHeader: true,
          TableColumns: [
            'name',
            'github_stars',
            'github_watchers',
            'updated_at',
            'description',
            'created_at',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  autoGroupColumnDef: {
    floatingFilter: true,
    filter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```
**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({agGridApi}: AdaptableReadyInfo) => {
  agGridApi.openToolPanel('columns');
};
```

### Pre-existing Column Filters

If there are pre-existing Column Filters on a Column which is being Row Grouped, these will, of course, affect which rows are displayed in the Row Grouped Column.

For instance, if the Column Filter means that just 2 rows pass as true, then only those 2 rows appear in the Row Grouped Column.

However, the Filter list for the Row Grouped Column will still display **all** distinct values (either as a list or Tree).

- This is because the Filter for the Row Grouped Column (in `RowGroupDisplayType`: single) is stored separately
- It is not stored using the Row-Grouped Column's Id, but with a "special" columnId of `ag-Grid-AutoColumn`

**Example: Row Grouping: Existing Filters**

Filtering Row Grouped Columns on Columns with existing Column Filters

 

- In this example we have provided 2 Column Filters - on the `Language` and `License` Columns, and we have also Row-Grouped on those 2 columns
- As a result we only see 12 rows in the Grid (insted of 25) and no Row Group for "TypeScript"
- However, when we open the `In` Filter dropdown for the Row Grouped Column we see all the distinct values (in a Tree structure), allowing us to create an **additional** Column Filter if we wish

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Grouping: Existing Column Filters',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout', 'ColumnFilter']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Single Grouped Layout',
      Layouts: [
        {
          Name: 'Row Grouped Layout',
          RowGroupedColumns: ['language', 'license'],
          RowGroupDisplayType: 'single',
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnFilters: [
            {
              ColumnId: 'language',
              Expression: '[language] IN ("JavaScript", "HTML")',
            },
            {
              ColumnId: 'license',
              Expression: 'ENDS_WITH([license], "se")',
            },
          ],
          SuppressAggFuncInHeader: true,
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'github_watchers',
            'license',
            'updated_at',
            'description',
            'created_at',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  autoGroupColumnDef: {
    floatingFilter: true,
    filter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```
**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({agGridApi}: AdaptableReadyInfo) => {
  agGridApi.openToolPanel('columns');
};
```

### Setting Grouped Column Filter

As noted above, when using `RowGroupDisplayType` of *single*, the Column Filter for the Row Grouped Column is stored separately to those of the columns which are being Row-Grouped.

It is stored using the special *columnId* of `ag-Grid-AutoColumn` (which is created dynamically by AG Grid).

This allows you to create a Column Filter just for the Row Grouped Column, irrespective of any Filters applied to the Columns being Row Grouped.

**Example: Row Grouping: Setting Row Grouped Column Filter**

Setting Filter for the Row Grouped Column

 

- In this example we explicitly set a [Column Filter](https://www.adaptabletools.com/docs/handbook-column-filter/index.md) on the Row Grouped Column using an `IN` [Expression](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md) which selects 2 group paths:
  - `"JavaScript"` - a single value, so the "JavaScript" group and all its sub-leaves and nodes are selected
  - `TO_ARRAY("HTML", "Other")` - a multi-segment *path*, so that only the "Other" leaf under "HTML" is selected
- The full Expression is `[ag-Grid-AutoColumn] IN ("JavaScript", TO_ARRAY("HTML", "Other"))`
- Because the Row Grouped Column is a tree of grouped values, each item in the `IN` list is a *path*: a single value matches a top-level group, while `TO_ARRAY(...)` describes a deeper path down the tree
- We have also set a larger Column Size for the Row Grouped Column to make it easier to see which items have been Filtered

- Open the Column Filter for the Row Grouped Column and note how the 2 items have been selected

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Grouping: Setting Row Group Column Filter',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout', 'ColumnFilter']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Single Grouped Layout',
      Layouts: [
        {
          Name: 'Row Grouped Layout',
          RowGroupedColumns: ['language', 'license'],
          RowGroupDisplayType: 'single',
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnFilters: [
            {
              ColumnId: 'ag-Grid-AutoColumn',
              Expression:
                '[ag-Grid-AutoColumn] IN ("JavaScript", TO_ARRAY("HTML", "Other"))',
            },
          ],
          SuppressAggFuncInHeader: true,
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'github_watchers',
            'license',
            'updated_at',
            'description',
            'created_at',
          ],
          ColumnSizing: {'ag-Grid-AutoColumn': {Width: 250}},
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  autoGroupColumnDef: {
    floatingFilter: true,
    filter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```
**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({agGridApi}: AdaptableReadyInfo) => {
  agGridApi.openToolPanel('columns');
};
```

### Filtering the AG Grid Field

AG Grid has a [neat option](https://www.ag-grid.com/javascript-data-grid/grouping-single-group-column/#configuration) that allows developers to set which Column's values will appear in the leaf rows of the Row Grouped Column.

This is done by settting the `field` property in the `autoGroupColumnDef` section in Grid Options

When this property is set, AdapTable will also apply the Tree List structure, allowing users easily to select from both the Column(s) being Row Grouped and the Column that is set as the `field`.

- It is also possible to set the Filter when Field is set
- This is done using the same array of arrays as when you have mutliple Row Grouped Columns

**Example: Row Grouping: Filtering with Field**

Setting Filter for the Row Grouped Column when Field is set

 

- In this example we have set the `field` property in `autoGroupColumnDef` to be the `Name` column
- As a result, AG Grid displays the `Name` Column's values when you open a Row Group
- AdapTable follows suit for the Row Grouped Column's Column Filter, allowing you to see the same structure (and to select values to filter from both Columns)

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Grouping: Filtering With Field',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout', 'ColumnFilter']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Row Grouped Layout',
      Layouts: [
        {
          Name: 'Row Grouped Layout',
          RowGroupedColumns: ['language'],
          RowGroupDisplayType: 'single',
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          SuppressAggFuncInHeader: true,
          TableColumns: [
            'github_stars',
            'github_watchers',
            'license',
            'updated_at',
            'description',
            'created_at',
          ],
          AutoSizeColumns: true,
        },
        {
          Name: 'Row Grouped Filtered Layout',
          RowGroupedColumns: ['language'],
          RowGroupDisplayType: 'single',
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnSizing: {'ag-Grid-AutoColumn': {Width: 350}},
          ColumnFilters: [
            {
              ColumnId: 'ag-Grid-AutoColumn',
              Expression:
                '[ag-Grid-AutoColumn] IN ("JavaScript", TO_ARRAY("TypeScript", "angular"), TO_ARRAY("TypeScript", "svelte"))',
            },
          ],
          SuppressAggFuncInHeader: true,
          TableColumns: [
            'github_stars',
            'github_watchers',
            'license',
            'updated_at',
            'description',
            'created_at',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  autoGroupColumnDef: {
    floatingFilter: true,
    filter: true,
    field: 'name',
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```
**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({agGridApi}: AdaptableReadyInfo) => {
  agGridApi.openToolPanel('columns');
};
```

## `RowGroupDisplayType`: Multi

When the `RowGroupDisplayType` is *multi*, AG Grid will display each Row Grouped Column as a separate Column.

Unlike with *single* Display Type, AdapTable does very little as far as Filtering is concerned. 

Column Filters are created, and persisted, in exactly the same way for "normal" and Row-Grouped Columns.

**Example: Row Grouping: Filtering Multi RowGroupDisplayType**

Filtering Row Grouped Columns with Display Type Multi

 

- In this example we have provided a Layout with `RowGroupDisplayType` set to *multi*
- We have Row Grouped on the `Language` and `License` Columns, but each appears as its own Column
- We have also created Column Filters on those 2 Columns which are evaluated, and displayed, identically irrespective of whether the Column is Row Grouped

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Grouping: Filtering DisplayType Multi',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout', 'ColumnFilter']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'DisplayType Multi Unfiltered',
      Layouts: [
        {
          Name: 'DisplayType Multi Filtered',
          RowGroupedColumns: ['language', 'license'],
          RowGroupValues: {
            RowGroupDefaultBehavior: 'always-expanded',
          },
          RowGroupDisplayType: 'multi',
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnFilters: [
            {
              ColumnId: 'language',
              Expression: '[language] IN ("JavaScript", "HTML")',
            },
            {
              ColumnId: 'license',
              Expression: 'ENDS_WITH([license], "se")',
            },
          ],
          SuppressAggFuncInHeader: true,
          TableColumns: [
            'language',
            'license',
            'name',
            'github_stars',
            'github_watchers',
            'updated_at',
            'description',
            'created_at',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  autoGroupColumnDef: {
    floatingFilter: true,
    filter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```
**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({agGridApi}: AdaptableReadyInfo) => {
  agGridApi.openToolPanel('columns');
};
```
