React Tree Grid Cell Merging
The Ignite UI for React Tree 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.
React Tree Grid 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).
<IgrTreeGrid data={data} cellMergeMode={cellMergeMode} >
...
</IgrTreeGrid>
const cellMergeMode: GridCellMergeMode = 'always';
Column Merge Toggle
At the column level, merging can be enabled or disabled with the merge property.
<IgrColumn field="OrderID" merge={true}></IgrColumn>
<IgrColumn field="ShipperName" merge={false}></IgrColumn>
In the above example:
- The OrderID column will merge adjacent duplicate values.
- The ShipperName column will render normally without merging.
Combined Example
<IgrTreeGrid data={data} cellMergeMode={cellMergeMode} autoGenerate={false}>
<IgrColumn field="OrderID" header="Order ID" merge={true}></IgrColumn>
<IgrColumn field="ShipperName" header="Shipper Name" merge={true}></IgrColumn>
<IgrColumn field="Salesperson" header="Salesperson"></IgrColumn>
</IgrTreeGrid>
const 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 Class
A custom merge strategy must implement the IgrGridMergeStrategy class:
merge- defines how merged cells are produced.comparer- defines the condition to decide if two adjacent records should be merged.
The IgrTreeGrid provides two built-in strategies that implement the IGridMergeStrategy interface: IgrDefaultTreeGridMergeStrategy and IgrByLevelTreeGridMergeStrategy. IgrDefaultTreeGridMergeStrategy merges all cells with the same value, regardless of their hierarchical level. In contrast, IgrByLevelTreeGridMergeStrategy only merges cells if they have the same value and are located at the same level, making level a required condition for merging.
Extending the Default Strategy
If you only want to customize part of the behavior (for example, the comparer logic), you can extend one of the built-in strategies, either IgrDefaultTreeGridMergeStrategy or IgrByLevelTreeGridMergeStrategy, and override the relevant methods.
export class MyCustomStrategy extends IgrDefaultTreeGridMergeStrategy {
/* 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:
<IgrTreeGrid data={data} mergeStrategy={customStrategy}>
<IgrColumn field="ActionID" merge={true}></IgrColumn>
<IgrColumn field="ProjectName" merge={true}></IgrColumn>
</IgrTreeGrid>
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.
- 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.
- 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.
API References
Additional Resources
- Filtering
- Excel Style Filtering
- Virtualization and Performance
- Paging
- Sorting
- Summaries
- Column Moving
- Column Pinning
- Column Resizing
- Selection
Our community is active and always welcoming to new ideas.