# Formatting Column Groups

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

- Columns in Column Groups can be formatted depending on whether the Group is Expanded or Collapsed

By default all Columns placed inside Column Groups will be [styled and formatted](https://www.adaptabletools.com/docs/handbook-column-formatting/index.md) normally, and irrespective of whether the Column Group is expanded or collapsed.

However it is possible to stipulate that a Column is only formatted if the Column Group in which it is situated is expanded (or collapsed).

This is particularly useful when using [Pivot Column Groups](https://www.adaptabletools.com/docs/handbook-layouts-pivot-column-groups/index.md) and [Pivot Total Columns](https://www.adaptabletools.com/docs/handbook-layouts-pivot-total-columns/index.md)

This is done using the `ColumnGroupScope` property in the Format Column object which can take 3 values:

- `Both` (the default) 
- `Expanded`
- `Collapsed`

`Both` will **not** be applied to top-level columns which don't have a parent group

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

Setting Column Groups to be Expanded or Collapsed

 

- In this example we have created [Format Column Styles](https://www.adaptabletools.com/docs/handbook-column-formatting-adaptable-style/index.md) on 3 Columns inside Column Groups - each with a different behaviour (set via the `ColumnGroupScope` property):
  - The `Issue Change` column is set to `Both` - which is why the Style is visible when the `Issues & PRs` Column Group is both expanded and collapsed
  - The `Github Stars` column is set to `Collapsed` - which is why the Style is visible when the `Details` Column Group is collapsed (but not expanded)
  - The `Github Watchers` column is set to `Expanded` - which is why the Style is visible when the `Details` Column Group is expanded (but not collapses)

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 Formatting',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'All Expanded',
      Layouts: [
        {
          TableColumns: [
            'name',
            'week_issue_change',
            'open_issues_count',
            'open_pr_count',
            'closed_issues_count',
            'closed_pr_count',
            'github_stars',
            'github_watchers',
            'created_at',
            'updated_at',
            'pushed_at',
            'language',
            'license',
            'description',
          ],
          Name: 'All Expanded',
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'always-expanded',
          },
          AutoSizeColumns: true,
        },
        {
          TableColumns: [
            'name',
            'week_issue_change',
            'open_issues_count',
            'open_pr_count',
            'closed_issues_count',
            'closed_pr_count',
            'github_stars',
            'github_watchers',
            'created_at',
            'updated_at',
            'pushed_at',
            'language',
            'license',
            'description',
          ],
          Name: 'All Collapsed',
          ColumnGroupValues: {
            ColumnGroupDefaultBehavior: 'always-collapsed',
          },
          AutoSizeColumns: true,
        },
      ],
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-week_issue_change',
          Scope: {
            ColumnIds: ['week_issue_change'],
          },
          Style: {
            BackColor: 'var(--ab-color-palette-24)',
            ForeColor: 'var(--ab-color-palette-14)',
            FontWeight: 'Bold',
            Alignment: 'Right',
          },
          ColumnGroupScope: 'Both',
        },
        {
          Name: 'formatColumn-github_stars',
          Scope: {
            ColumnIds: ['github_stars'],
          },
          Style: {
            BackColor: 'var(--ab-color-palette-19)',
            ForeColor: 'var(--ab-color-palette-14)',
            FontWeight: 'Bold',
            Alignment: 'Right',
          },
          ColumnGroupScope: 'Collapsed',
        },
        {
          Name: 'formatColumn-github_watchers',
          Scope: {
            ColumnIds: ['github_watchers'],
          },
          Style: {
            BackColor: 'var(--ab-color-palette-30)',
            ForeColor: 'var(--ab-color-palette-14)',
            FontWeight: 'Bold',
            Alignment: 'Right',
          },
          ColumnGroupScope: 'Expanded',
        },
      ],
    },
  },
};
```

**`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: 'week_issue_change',
        headerName: 'Issue Change',
        cellDataType: 'number',
        enableValue: true,
      },
      {
        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: 'Details',
    marryChildren: true,
    groupId: 'details',
    children: [
      {
        field: 'github_stars',
        headerName: 'GitHub Stars',
        cellDataType: 'number',
        enableValue: true,
      },
      {
        field: 'github_watchers',
        headerName: 'GitHub Watchers',
        cellDataType: 'number',
        enableValue: true,
      },
      {
        field: 'created_at',
        headerName: 'Created',
        cellDataType: 'date',
        columnGroupShow: 'closed',
      },

      {
        field: 'updated_at',
        headerName: 'Updated',
        cellDataType: 'date',
        columnGroupShow: 'closed',
      },
      {
        field: 'pushed_at',
        headerName: 'Pushed',
        cellDataType: 'date',
        columnGroupShow: 'closed',
      },
    ],
  },
  {
    field: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    enableValue: true,
    columnGroupShow: 'open',
  },
  {
    field: 'github_watchers',
    headerName: 'GitHub Watchers',
    cellDataType: 'number',
    enableValue: true,
    columnGroupShow: 'closed',
  },

  {
    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,
  },
];
```
