# Column Grouping

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

- Column Groups are fully supported in AdapTable
- The Layout Wizard provides details of Column Groups and respects any Column Grouping rules

AdapTable fully supports [AG Grid Column Grouping](https://www.ag-grid.com/javascript-data-grid/column-groups/) (sometimes known as column banding).

This is where multiple columns are placed in one group and given an additional, common Header.

**Example: Column Groups**

Using Column Grouping in AdapTable

 

- 3 Column Groups have been defined in `colDefs` in Grid Options:
- `Issues & PRs`
  - contains `Open PRs`, `Closed PRs`, `Open Issues` and `Closed Issues` Columns 
  - has `marryChildren` set to *true* - so columns **cannot** be out of the Group
  - `Open Issues` and `Closed Issues` Columns set with `columnGroupShow` as 'closed'
- `Dates` 
  - contains 3 Date Columns: `Updated`, `Pushed`, `Created` 
  - has `marryChildren` set to *false* - so columns **can** be out of the Group
- `Details`
  - contains `Description`, `License`, `Has Wiki`, `Has Projects` & `Has Pages` Columns
  - has `marryChildren` set to *true*  - so columns **cannot** be out of the Group
  - the `Description` column has `columnGroupShow` set to 'closed'

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
  rowData.ts            [template]
  styles.css            [template]
  tickingDataHelper.ts  [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',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'created_at',
            'description',
            'license',
            'has_wiki',
            'has_projects',
            'has_pages',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          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,
    children: [
      {
        field: 'open_pr_count',
        headerName: 'Open PRs',
        cellDataType: 'number',
      },
      {
        field: 'closed_pr_count',
        headerName: 'Closed PRs',
        cellDataType: 'number',
      },
      {
        field: 'open_issues_count',
        headerName: 'Open Issues',
        cellDataType: 'number',
        columnGroupShow: 'closed',
      },
      {
        field: 'closed_issues_count',
        headerName: 'Closed Issues',
        cellDataType: 'number',
        columnGroupShow: 'closed',
      },
    ],
  },
  {
    field: 'language',
    cellDataType: 'text',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    headerName: 'Dates',
    marryChildren: false,
    children: [
      {
        field: 'created_at',
        headerName: 'Created',
        cellDataType: 'date',
      },

      {
        field: 'updated_at',
        headerName: 'Updated',
        cellDataType: 'date',
      },
      {
        field: 'pushed_at',
        headerName: 'Pushed',
        cellDataType: 'date',
      },
    ],
  },
  {
    headerName: 'Details',
    marryChildren: true,
    children: [
      {
        field: 'description',
        cellDataType: 'text',
        sortable: false,
        columnGroupShow: 'closed',
      },
      {
        field: 'license',
        cellDataType: 'text',
        editable: false,
        enablePivot: true,
        enableRowGroup: true,
      },

      {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: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'github_watchers',
    headerName: 'GitHub Watchers',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'week_issue_change',
    headerName: 'Issue Change',
    cellDataType: 'number',
    enableValue: true,
  },
];
```

**`src/onAdaptableReady.ts`**

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

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  tickNumericData(adaptableApi, 200, [
    'github_stars',
    'github_watchers',
    'open_issues_count',
    'closed_issues_count',
    'open_prs_count',
    'closed_prs_count',
  ]);
};
```

## Layouts

Column Groups are not specified in [Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md) - as the GridOptions definition is sufficient. 

But they are **respected** in Layouts.

For instance, in the Column list the Layout Editor displays the name of any Column Group a column belongs to.

It also prevents users moving columns out of Column Groups, where this is not allowed in the AG Grid definition.

## Column Names

By default AdapTable will not change the `FriendlyName` of a Column when it is in a Column Group.

The Friendly Name is how AdapTable refers to the Column e.g. in Wizards or Expression Editor

However sometimes the same Friendly Name can appear in multiple Column Groups (e.g. 3 columns groups each have a `Bid` and `Ask` column).

When this is the case, there are 2 ways to make the FriendlyName unique:

### Adding Column Group Name

The simplest way to ensure uniqueness of FriendlyName for Columns in Column Groups is via the `addColumnGroupToColumnFriendlyName` property in [Column Options](https://www.adaptabletools.com/docs/technical-reference-column/index.md).

This will append the name of the Column Group to the Friendly Name (e.g. "Bid [Bbg]", "Bid[Markit]").

### Using a property

Use the `columnFriendlyName` property also in [Column Options](https://www.adaptabletools.com/docs/technical-reference-column/index.md).

See the [Developer Tutorial on Configuring Column Headers](https://www.adaptabletools.com/docs/dev-guide-columns-column-headers/index.md) for more details

## Special Columns

AdapTable provides 3 "Special Columns" which are created dynamically.

- [Calculated Column](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md)
- [Action Column](https://www.adaptabletools.com/docs/handbook-action-column/index.md)
- [FreeText Column](https://www.adaptabletools.com/docs/handbook-freetext-column/index.md)

These can also be included in Column Groups, by providing these 2 steps:

- Add them explicitly in AG Grid Columns Defs (this is not normally required for Special Columns)
- Provide them with a [Column Type](https://www.adaptabletools.com/docs/dev-guide-columns-column-types/index.md) of `calculatedColumn`, `freeTextColumn` or `actionColumn`

**Example: Column Groups with Special Columns**

Column Groups containing Calculated, FreeText or Action Columns

 

- This example contains a Column Group (called 'Special Columns') which includes 3 Special Columns:
  - `Big Stars` - a [Calculated Column](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md)
  - `Add Issue` - an [Action Column](https://www.adaptabletools.com/docs/handbook-action-column/index.md)
  - `Comments` - a [FreeText Column](https://www.adaptabletools.com/docs/handbook-freetext-column/index.md)
- All 3 columns were defined in Column Defs (as well as Adaptable Options / Initial Adaptable State) 
- And all 3 columns were given a matching [Column Type](https://www.adaptabletools.com/docs/dev-guide-columns-column-types/index.md) to wire it all up together

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 {
  ActionColumnContext,
  AdaptableOptions,
  CellUpdateRequest,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Column Grouping with Special Columns',
  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'add_issue',
        friendlyName: 'Add Issue',
        controls: [
          {
            type: 'button',
            label: 'Add',
            style: {variant: 'outlined', tone: 'info'},
            onClick: (context: ActionColumnContext) => {
              const rowData: any = context.rowNode?.data;
              const cellUpdateRequest: CellUpdateRequest = {
                columnId: 'week_issue_change',
                newValue: rowData.week_issue_change + 1,
                primaryKeyValue: context.primaryKeyValue,
                rowNode: context.rowNode,
              };
              context.adaptableApi.gridApi.setCellValue(cellUpdateRequest);
            },
          },
        ],
      },
    ],
  },
  initialState: {
    Dashboard: {
      ModuleButtons: ['CalculatedColumn', 'FormatColumn'],
    },

    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'github_stars',
            'bigStars',
            'add_issue',
            'comments',
            'week_issue_change',
            'github_watchers',
            'language',
            'license',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
    CalculatedColumn: {
      CalculatedColumns: [
        {
          ColumnId: 'bigStars',
          Expression: '[github_stars] * 10 ',
          FriendlyName: 'Big Stars',
          CalculatedColumnSettings: {DataType: 'number'},
        },
      ],
    },

    FreeTextColumn: {
      FreeTextColumns: [
        {
          ColumnId: 'comments',
          FriendlyName: 'Comments',

          FreeTextStoredValues: [
            {PrimaryKey: 24195339, FreeText: 'Used by the US team'},
            {PrimaryKey: 224663696, FreeText: 'My personal favourite'},
            {PrimaryKey: 82095231, FreeText: 'Required by Support Team'},
          ],
          FreeTextColumnSettings: {
            Aggregatable: false,
            DataType: 'text',
          },
        },
      ],
    },
  },
};
```

**`src/columnDefs.ts`**

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

export const columnDefs: (ColDef | ColGroupDef)[] = [
  {
    field: 'id',
    cellDataType: 'number',
    hide: true,
    editable: false,
  },
  {
    field: 'name',
    cellDataType: 'text',
    sortable: true,
  },

  {
    field: 'language',
    cellDataType: 'text',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'license',
    cellDataType: 'text',
    editable: false,
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    headerName: 'Special Columns',
    children: [
      {
        colId: 'bigStars',
        type: ['calculatedColumn'],
        cellDataType: 'number',
      },
      {
        colId: 'add_issue',
        type: ['actionColumn'],
      },
      {
        colId: 'comments',
        type: ['freeTextColumn'],
        cellDataType: 'text',
      },
    ],
  },
  {
    field: 'week_issue_change',
    headerName: 'Issue Change',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'created_at',
    headerName: 'Created',
    cellDataType: 'date',
  },
  {
    field: 'has_wiki',
    headerName: 'Has Wiki',
    cellDataType: 'boolean',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'updated_at',
    headerName: 'Updated',
    cellDataType: 'date',
  },
  {
    field: 'pushed_at',
    headerName: 'Pushed',
    cellDataType: 'date',
  },
  {
    field: 'github_watchers',
    headerName: 'GitHub Watchers',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'description',
    cellDataType: 'text',
    sortable: false,
  },
  {
    field: 'open_issues_count',
    headerName: 'Open Issues',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_issues_count',
    headerName: 'Closed Issues',
    cellDataType: 'number',
    enableValue: true,
  },

  {
    field: 'open_pr_count',
    headerName: 'Open PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_pr_count',
    headerName: 'Closed PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {field: 'has_projects', headerName: 'Has Projects', cellDataType: 'boolean'},
  {field: 'has_pages', headerName: 'Has Pages', cellDataType: 'boolean'},
  {
    field: 'topics',
    cellDataType: 'textArray',
    editable: false,
    sortable: false,
  },
];
```
