Angular Cell Merging
The Ignite UI for Angular Hierarchical Grid provides a Cell Merging feature that combines two or more adjacent cells with the same value into a single, larger cell. Merging is applied vertically within a column and helps improve readability by reducing duplicate values. The feature can be configured to merge cells either by default matching data values or by applying a custom condition.
Angular Cell Merging Example
Enabling and Using Cell Merging
Cell merging in the grid is controlled at two levels:
- Grid-level merge mode – determines when merging is applied.
- Column-level merge toggle – determines which columns can merge cells.
Grid Merge Mode
The grid exposes a cellMergeMode
property that accepts values from the GridCellMergeMode
enum:
always
- Merges any adjacent cells that meet the merging condition, regardless of sort state.onSort
- Merges adjacent cells only when the column is sorted (default value).
<igx-hierarchical-grid [data]="data" [cellMergeMode]="cellMergeMode">
...
</igx-hierarchical-grid>
protected cellMergeMode: GridCellMergeMode = 'always';
Column Merge Toggle
At the column level, merging can be enabled or disabled with the merge
property.
<igx-column field="OrderID" [merge]="true"></igx-column>
<igx-column field="ShipperName" [merge]="false"></igx-column>
In the above example:
- The OrderID column will merge adjacent duplicate values.
- The ShipperName column will render normally without merging.
Combined Example
<igx-hierarchical-grid [data]="data" [cellMergeMode]="cellMergeMode" [autoGenerate]="false">
<igx-column field="OrderID" header="Order ID" [merge]="true"></igx-column>
<igx-column field="ShipperName" header="Shipper Name" [merge]="true"></igx-column>
<igx-column field="Salesperson" header="Salesperson"></igx-column>
</igx-hierarchical-grid>
protected cellMergeMode: GridCellMergeMode = 'onSort';
Here, the grid is set to merge only when columns are sorted, and both Category and Product columns are configured for merging.
Custom Merge Conditions
In addition to the built-in always
and onSort
modes, the grid allows you to define a custom condition for merging cells through the mergeStrategy
property. This strategy controls both how cells are compared and how merged ranges are calculated.
Merge Strategy Interface
A custom merge strategy must implement the IGridMergeStrategy
interface:
export interface IGridMergeStrategy {
merge: (
data: any[],
field: string,
comparer: (prevRecord: any, currentRecord: any, field: string) => boolean,
result: any[],
activeRowIndex?: number,
grid?: GridType
) => any[];
comparer: (prevRecord: any, record: any, field: string) => boolean;
}
merge
- defines how merged cells are produced.comparer
- defines the condition to decide if two adjacent records should be merged.
Extending the Default Strategy
If you only want to customize part of the behavior (for example, the comparer logic), you can extend the built-in DefaultMergeStrategy
and override the relevant methods.
export class MyCustomStrategy extends DefaultMergeStrategy {
/* Merge only cells within their respective projects */
public override comparer(prevRecord: any, record: any, field: string): boolean {
const a = prevRecord[field];
const b = record[field];
const projA = prevRecord['ProjectName'];
const projB = record['ProjectName'];
return a === b && projA === projB;
}
}
Applying a Custom Strategy
Once defined, assign the strategy to the grid through the mergeStrategy
property:
<igx-hierarchical-grid [data]="data" [mergeStrategy]="customStrategy">
<igx-column field="ActionID" [merge]="true"></igx-column>
<igx-column field="ProjectName" [merge]="true"></igx-column>
</igx-hierarchical-grid>
protected customStrategy = new MyCustomStrategy();
Feature Integration
Due to the specific behavior of merged cells it has to be noted how exactly it ties together with some of the other features of the grid:
- Excel export: merged cells remain merged when exported to Excel.
- Column pinning: cells remain merged when a column is pinned and are displayed in the pinned area.
- Row pinning: cells merge only withing their containing area, i.e. cells of pinned rows merge only with cells of other pinned rows, while cells of unpinned rows merge only with cells of unpinned rows.
- Navigation/Activation: when a cell is active, all merged cells in the same row become single cells, i.e. their merge sequence is broken. This also includes activation via keyboard navigation.
Note
If a merged cell is clicked, the closest cell from the merge sequence will become active.
- Updating/Editing: since activation breaks the merge sequence, only a single cell will be in edit mode.
- Row selection: if selected rows intersect merged cells, all related merged cells should be marked as part of the selection.