# Implementing Collapsible Column Groups in Kendo UI for Angular Grid

## Environment

<table>
    <tbody>
        <tr>
            <td>Product</td>
            <td>Progress® Kendo UI® for Angular Grid</td>
        </tr>
    </tbody>
</table>

## Description

I want to implement collapsible [column groups](https://www.telerik.com/kendo-angular-ui/components/grid/columns/headers) in the Kendo UI for Angular Grid. The Grid contains multiple columns organized into column groups, and users should be able to collapse groups to display a summary column or expand them to show detailed columns.

This Knowledge Base article also answers the following questions:

* How can I make column headers collapsible in Angular Grid?
* Is it possible to toggle column group visibility in Kendo UI for Angular Grid?
* Can I hide or show child columns dynamically in Angular Grid?

## Solution

To implement collapsible column groups, use a custom [header template](https://www.telerik.com/kendo-angular-ui/components/grid/columns/templates#header-template) for the column group to create a clickable toggle, and apply the [`hidden`](https://www.telerik.com/kendo-angular-ui/components/grid/api/columncomponent#hidden) property to the child columns to control their visibility.

1. Track the collapsed state of column groups using a `Set` that stores their titles:

   ```typescript
   public collapsedGroups = new Set<string>();
   ```

2. Create a method to toggle the collapsed state by checking if a group title exists in the `Set`:

   ```typescript
   public toggleGroup(groupTitle: string): void {
     if (this.collapsedGroups.has(groupTitle)) {
       this.collapsedGroups.delete(groupTitle);
     } else {
       this.collapsedGroups.add(groupTitle);
     }
   }
   ```

3. Create a custom clickable header for the column group using the [`kendoGridHeaderTemplate`](https://www.telerik.com/kendo-angular-ui/components/grid/api/headertemplatedirective) directive. When the header is clicked, call the `toggleGroup()` method with the column group title:

   ```html
   <kendo-grid-column-group title="Unit">
     <ng-template kendoGridHeaderTemplate let-column>
       <div (click)="toggleGroup(column.title)">
         <kendo-svg-icon
           [icon]="collapsedGroups.has(column.title) ? chevronRightIcon : chevronDownIcon"
         ></kendo-svg-icon>
         {{ column.title }}
       </div>
     </ng-template>
   </kendo-grid-column-group>
   ```

4. Define the detail columns that should appear when the group is expanded and set their [`hidden`](https://www.telerik.com/kendo-angular-ui/components/grid/api/columncomponent#hidden) property to `true` when the group title is contained in the `collapsedGroups` set:

   ```html
   <kendo-grid-column-group title="Unit">
     
     <kendo-grid-column
       field="UnitPrice"
       title="Unit Price"
       [width]="120"
       [hidden]="collapsedGroups.has('Unit')"
     ></kendo-grid-column>
   </kendo-grid-column-group>
   ```

5. Add a summary column that appears when the group is collapsed and apply the opposite condition to hide it when the group title is not in the `collapsedGroups` set. Use [a custom cell template](https://www.telerik.com/kendo-angular-ui/components/grid/columns/templates#cell-template) to display a combined summary of the detail columns:

   ```html
   <kendo-grid-column-group title="Unit">
     
     <kendo-grid-column
       [width]="240"
       title="Summary"
       [hidden]="!collapsedGroups.has('Unit')"
     >
       <ng-template kendoGridCellTemplate let-dataItem>
         {{ dataItem.UnitsInStock }} units ({{ (dataItem.UnitsInStock * dataItem.UnitPrice) }})
       </ng-template>
     </kendo-grid-column>
   </kendo-grid-column-group>
   ```

The following example demonstrates the proposed approach in action:

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/knowledge-base/grid-collapsible-column-groups/source.txt)

## See Also

* [Multi-Column Headers in Kendo UI for Angular Grid](https://www.telerik.com/kendo-angular-ui/components/grid/columns/headers)
* [API Reference of the GridComponent](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent)
* [API Reference of the ColumnGroupComponent](https://www.telerik.com/kendo-angular-ui/components/grid/api/columngroupcomponent)
* [API Reference of the ColumnComponent](https://www.telerik.com/kendo-angular-ui/components/grid/api/columncomponent)
