# Column Groups Expanded Collapsed Behaviour

Canonical page: https://www.adaptabletools.com/docs/handbook-grouping-columns-expanded-collapsed

- Column Groups in AdapTable can be configured to open as Expanded or Collapsed
- Users are able to set exceptions to the default for certain Column Groups

Developers can configure, when a Layout loads, the collapsed / expanded behaviour for Column Groups.

This is done on a per-Layout basis using the `ColumnGroupValues` property in the [`Base Layout`](https://www.adaptabletools.com/docs/reference/layoutbase.md) object.

Something very similar can be done for [Row Groups](https://www.adaptabletools.com/docs/handbook-grouping-rows/index.md) using the (almost identical) `RowGroupValues` property

### Understanding the ColumnGroupValues object

The [`ColumnGroupValues`](https://www.adaptabletools.com/docs/reference/columngroupvalues.md) object consists of 2 alternative properties (only one of which can be chosen):
- `ColumnGroupDefaultBehavior` - to set **default and consistent behaviour**
- `ColumnGroupValuesWithExceptionKeys` - (optional) to list any specific **exceptions** as an array

This allows you either to configure behaviour that never changes or to list exceptions.

<b>Consistent Behaviour</b>

If you want the behaviour of Column Groups to be consistent (i.e. never change and never save exceptions) then use the `ColumnGroupDefaultBehavior` option in `ColumnGroupValues`.

The `ColumnGroupDefaultBehavior` property consists of 2 possible values:

- `always-expanded`
- `always-collapsed`

```ts
ColumnGroupValues: {
  ColumnGroupDefaultBehavior: 'always-expanded',
},
```

<b>List Exceptions</b>

If you wish to provide a default behaviour, but also set or save exceptions, use `ColumnGroupValuesWithExceptionKeys` property in `ColumnGroupValues`.

The [`ColumnGroupValuesWithExceptionKeys`](https://www.adaptabletools.com/docs/reference/columngroupvalueswithexceptionkeys.md) object contains 2 properties:

- `ColumnGroupDefaultBehavior` - contains 2 possible values used to set **default** behaviour: `expanded` and `collapsed`
- `ExceptionGroupKeys` - lists the Ids of Column Groups which are exceptions to default behaviour, provided as an array:

- Make sure to set the `groupId` property for the Column Group, if you wish to use it in Exceptions
- e.g in Example below we will have set the `groupId` property for the Column Groups of Prices and Details

```ts
// Set all Column Groups to be expanded except for Prices and Details
ColumnGroupValues: {
  ColumnGroupDefaultBehavior: 'collapsed',
  ExceptionGroupKeys: ['Prices', 'Details'],
},
```

There are 4 posible use cases:

| Use Case                                        | Default Behaviour  | Provide Exceptions |
| ----------------------------------------------- | ------------------ | :----------------: |
| All Groups are expanded                          | `always-expanded`  |         ❌          |
| All Groups are collapsed                        | `always-collapsed` |         ❌          |
| All Groups are expanded, but exceptions listed  | `expanded`         |         ✅          |
| All Groups are collapsed, but exceptions listed | `collapsed`         |         ✅          |

**Example: Column Groups: Expanded and Collapsed**

Setting Column Groups to be Expanded or Collapsed

 

- This example illustrates the different options for Expanding / Collapsing Column Groups.  We provide 3 Column Groups:
  - `Github` - which shows `Github Starts` when expanded and `Github Watchers` when closed
  - `Issues & PRs` - which shows the 2 Issues columns when expanded and the 2 PRs columns when closed
  - `Dates` - which shows `Created` when expanded and `Updated` and `Pushed` when closed
- We also provide 4 Table Layouts which demonstrate the different options:
  - `Always Expanded` - has ColumnGroupDefaultBehavior set to `always-expanded`
  - `Always Collapsed` - has ColumnGroupDefaultBehavior set to `always-collapsed`
  - `Collapsed Exceptions` - all Column Groups collapsed with exception of `Issues & PRs`
  - `Expanded Exceptions` - all Column Groups expanded with exception of `Issues & PRs`

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts
  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: 'Column Grouping Expand / Collapsed',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Always Expanded',
      Layouts: [
        {
          TableColumns: [
            'name',
            'github_stars',
            'github_watchers',
            'open_issues_count',
            'open_pr_count',
            'closed_issues_count',
            'closed_pr_count',
            'created_at',
            'updated_at',
            'pushed_at',
            'language',
            'license',
            'description',
          ],
          Name: 'Always Expanded',
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'always-expanded',
          },
          AutoSizeColumns: true,
        },
        {
          TableColumns: [
            'name',
            'github_stars',
            'github_watchers',
            'open_issues_count',
            'open_pr_count',
            'closed_issues_count',
            'closed_pr_count',
            'created_at',
            'updated_at',
            'pushed_at',
            'language',
            'license',
            'description',
          ],
          Name: 'Always Collapsed',
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'always-collapsed',
          },
          AutoSizeColumns: true,
        },
        {
          TableColumns: [
            'name',

            'github_stars',
            'github_watchers',
            'open_issues_count',
            'open_pr_count',
            'closed_issues_count',
            'closed_pr_count',
            'created_at',
            'updated_at',
            'pushed_at',
            'language',
            'license',
            'description',
          ],
          Name: 'Collapsed Exceptions',
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'collapsed',
            ExceptionGroupKeys: ['issues_prs'],
          },
          AutoSizeColumns: true,
        },
        {
          TableColumns: [
            'name',

            'github_stars',
            'github_watchers',
            'open_issues_count',
            'open_pr_count',
            'closed_issues_count',
            'closed_pr_count',
            'created_at',
            'updated_at',
            'pushed_at',
            'language',
            'license',
            'description',
          ],
          Name: 'Expanded Exceptions',
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'expanded',
            ExceptionGroupKeys: ['issues_prs'],
          },
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/columnDefs.ts`**

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

export const columnDefs: (ColDef | ColGroupDef)[] = [
  {
    field: 'id',
    cellDataType: 'number',
    hide: true,
    editable: false,
  },
  {
    field: 'name',
    cellDataType: 'text',
  },
  {
    headerName: 'Issues & Prs',
    marryChildren: true,
    groupId: 'issues_prs',
    children: [
      {
        field: 'open_pr_count',
        headerName: 'Open PRs',
        cellDataType: 'number',
        columnGroupShow: 'open',
      },
      {
        field: 'open_issues_count',
        headerName: 'Open Issues',
        cellDataType: 'number',
        columnGroupShow: 'open',
      },
      {
        field: 'closed_pr_count',
        headerName: 'Closed PRs',
        cellDataType: 'number',
        columnGroupShow: 'closed',
      },
      {
        field: 'closed_issues_count',
        headerName: 'Closed Issues',
        cellDataType: 'number',
        columnGroupShow: 'closed',
      },
    ],
  },
  {
    headerName: 'Dates',
    marryChildren: true,
    groupId: 'dates',
    children: [
      {
        field: 'created_at',
        headerName: 'Created',
        cellDataType: 'date',
        columnGroupShow: 'open',
      },

      {
        field: 'updated_at',
        headerName: 'Updated',
        cellDataType: 'date',
        columnGroupShow: 'closed',
      },
      {
        field: 'pushed_at',
        headerName: 'Pushed',
        cellDataType: 'date',
        columnGroupShow: 'closed',
      },
    ],
  },
  {
    headerName: 'Github',
    marryChildren: true,
    groupId: 'github',
    children: [
      {
        field: 'github_stars',
        headerName: 'GitHub Stars',
        cellDataType: 'number',
        enableValue: true,
        columnGroupShow: 'open',
      },
      {
        field: 'github_watchers',
        headerName: 'GitHub Watchers',
        cellDataType: 'number',
        enableValue: true,
        columnGroupShow: 'closed',
      },
    ],
  },
  {
    headerName: 'Details',
    marryChildren: true,
    groupId: 'details',
    children: [
      {
        field: 'license',
        cellDataType: 'text',
        editable: false,
        enablePivot: true,
        enableRowGroup: true,
        columnGroupShow: 'open',
      },
      {
        field: 'language',
        cellDataType: 'text',
        enablePivot: true,
        enableRowGroup: true,
        columnGroupShow: 'open',
      },

      {
        field: 'description',
        cellDataType: 'text',
        sortable: false,
        columnGroupShow: 'closed',
      },
    ],
  },

  {field: 'has_wiki', headerName: 'Has Wiki', cellDataType: 'boolean'},
  {
    field: 'has_projects',
    headerName: 'Has Projects',
    cellDataType: 'boolean',
  },
  {field: 'has_pages', headerName: 'Has Pages', cellDataType: 'boolean'},

  {
    field: 'week_issue_change',
    headerName: 'Issue Change',
    cellDataType: 'number',
    enableValue: true,
  },
];
```

## Pivot Layouts

The same behaviour is also available for the Pivot Column Groups which are displayed in [Pivot Layouts](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md).

See [Pivot Layout: Pivot Column Groups](https://www.adaptabletools.com/docs/handbook-layouts-pivot-column-groups/index.md) for more detailed information and a demo (similar to the one above)
