# Pivot Column Groups

Canonical page: https://www.adaptabletools.com/docs/handbook-layouts-pivot-column-groups

This AdapTable Help Page is a work in progress and will be completed in the next couple of days

[Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md) · [Pivot Layouts](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md) · [All Pivot Layout Topics](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md#pivot-layout-topics).
Column group expand/collapse in table mode — [Expanded / Collapsed Column Groups](https://www.adaptabletools.com/docs/handbook-grouping-columns-expanded-collapsed/index.md).

Pivot Column Groups are created dynamically by AG Grid when in pivot mode.

One Column Group is created for each distinct value in the Pivot Column.

- In the demo below 3 Pivot Columns Groups are created dynamically by AG Grid
- e.g. one Pivot Column Group for each of the 3 values in the `Language` Column: HTML, JavaScript, TypeScript

If there is more than one Pivot Column, Pivot Column Groups are created at each level of nesting.

The demo below has a Pivot Column Group for each nested level (e.g. HTML/MIT License, JavaScript/MIT License etc)

## Expanding Column Groups

By default AdapTable will display all the columns which are dynamically created by a Pivot Column.

In other words, it will automatically expand, and display, all Pivot Column Groups.

If this is not the desired behaviour, AdapTable offers 2 options to change this:

- Setting Expanded / Collapsed Behavour for Pivot Columns Groups
- Setting the Pivot Expand Level

- These are **mutually exclusive** ways of setting Column Groups Expansion
- Choose one or the other, but not both

### Expanded Collapsed Behaviour

The expanded / collapsed behaviour for Pivot Column Groups can be configured in exactly the same way as for "normal" [Column Groups](https://www.adaptabletools.com/docs/handbook-grouping-columns/index.md);  in other words they can be set in one of 4 ways:
- to always open expanded
- to always open collapsed
- to open expanded by default (but with exceptions)
- to open collapsed by default (but with exceptions)

- Pivot Column Groups can only be Expanded or Collapsed if the Group is more than one level deep
- In othere words, at least 2 Pivot Columns must be set

See [Column Groups: Expanded / Collapsed Behaviour](https://www.adaptabletools.com/docs/handbook-grouping-columns-expanded-collapsed/index.md) for more information

**Example: Pivot Column Groups: Expanded Collapsed**

Configuring Expanded Collapsed Behaviour for Pivot Column Groups

 

- This example contains 4 Layouts each of which show different options for Expanded / Collapsed Pivot Column Groups:
  - `Always Expanded` - all Pivot Column Groups are expanded (because ColumnGroupDefaultBehavior is set to `always-expanded`)
  - `Always Collapsed` - all Pivot Column Groups are collapsed (because ColumnGroupDefaultBehavior is set to `always-collapsed`)
  - `Collapsed Exceptions` - all Pivot Column Groups are collapsed with exception of `pivotGroup_language-license_HTML` (first group)
  - `Expanded Exceptions` - all Pivot Column Groups are expanded with exception of `pivotGroup_language-license_HTML` (first group)
- Note: We have set 2 Pivot Columns (`Language` and `License`) which is why the Pivot Column Groups can be expanded or collapsed

- In one of the 2 Exceptions Layouts, expand / collapse Pivot Column Groups, switch Layouts and then switch back
- Note: the Exceptions have been saved, and are re-applied when the Layout loads

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  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: 'Pivot Column Groups: Expanded / Collapsed',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Always Expanded',
      Layouts: [
        {
          Name: 'Always Expanded',
          PivotColumns: ['language', 'license'],
          PivotGroupedColumns: ['has_wiki'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
          ],
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'always-expanded',
          },
        },
        {
          Name: 'Always Collapsed',
          PivotColumns: ['language', 'license'],
          PivotGroupedColumns: ['has_wiki'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
          ],
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'always-collapsed',
          },
        },
        {
          Name: 'Expanded Exceptions',
          PivotColumns: ['language', 'license'],
          PivotGroupedColumns: ['has_wiki'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
          ],
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'expanded',
            ExceptionGroupKeys: ['pivotGroup_language-license_HTML'],
          },
        },

        {
          Name: 'Collapsed Exceptions',
          PivotColumns: ['language', 'license'],
          PivotGroupedColumns: ['has_wiki'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
          ],
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'collapsed',
            ExceptionGroupKeys: ['pivotGroup_language-license_HTML'],
          },
        },
      ],
    },
  },
};
```

### Pivot Expand Level

An alternative to setting the expanded / collapsed behaviour described above is to set the Expand Level.

This is done via the `PivotExpandLevel` property in the Layout Definition, which can be used to configure these columns as follows:

- -1 to expand all (the default behaviour)
- 0 for no expanded Column Groups
- 1 expand just the 1st level (and so on...)

**Example: Expanding Pivot Columns**

Pivot Layout with Expanded Pivot Columns configured

 

- This example contains 2 Pivot Layout with default `PivotExpandLevel` settings:
  - `Pivot Layout Collapsed` - set to 0 and so we don't see the `Language` Column Groups
  - `Pivot Layout Expanded` - set to 1 and therefore we do see the `Language` Column Groups

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: 'Expand Pivot Columns',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Pivot Layout Collapsed',
      Layouts: [
        {
          Name: 'Pivot Layout Collapsed',
          PivotColumns: ['license', 'language'],
          PivotExpandLevel: 0,
          PivotGroupedColumns: ['has_wiki'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
        },
        {
          Name: 'Pivot Layout Expanded',
          PivotColumns: ['license', 'language'],
          PivotExpandLevel: 1,
          PivotGroupedColumns: ['has_wiki'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
          ],
        },
      ],
    },
  },
};
```

**`src/gridOptions.ts`**

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

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

export const gridOptions: GridOptions<WebFramework> = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: 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');
};
```
