Skip to content

Commit e471b3a

Browse files
authored
feat(cell-merge): add cell merge code snippets for WC (#1750)
1 parent cd6cbdf commit e471b3a

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

doc/en/components/grids/_shared/cell-merging.md

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,23 @@ The grid exposes a `cellMergeMode` property that accepts values from the `GridCe
3434
```tsx
3535
const cellMergeMode: GridCellMergeMode = 'always';
3636
```
37+
38+
```html
39+
<{ComponentSelector} cell-merge-mode="always">
40+
...
41+
</{ComponentSelector}>
42+
```
3743
### Column Merge Toggle
3844
At the column level, merging can be enabled or disabled with the `merge` property.
3945

4046
```tsx
4147
<IgrColumn field="OrderID" merge={true}></IgrColumn>
4248
<IgrColumn field="ShipperName" merge={false}></IgrColumn>
4349
```
50+
```html
51+
<igc-column field="OrderID" merge="true"></igc-column>
52+
<igc-column field="ShipperName" merge="false"></igc-column>
53+
```
4454

4555
In the above example:
4656
- The **OrderID** column will merge adjacent duplicate values.
@@ -49,7 +59,7 @@ In the above example:
4959
### Combined Example
5060

5161
```tsx
52-
<{ComponentSelector} data={data} cellMergeMode]={cellMergeMode} autoGenerate={false}>
62+
<{ComponentSelector} data={data} cellMergeMode={cellMergeMode} autoGenerate={false}>
5363
<IgrColumn field="OrderID" header="Order ID" merge={true}></IgrColumn>
5464
<IgrColumn field="ShipperName" header="Shipper Name" merge={true}></IgrColumn>
5565
<IgrColumn field="Salesperson" header="Salesperson"></IgrColumn>
@@ -58,14 +68,22 @@ In the above example:
5868
```tsx
5969
const cellMergeMode: GridCellMergeMode = 'onSort';
6070
```
71+
```html
72+
<{ComponentSelector} cell-merge-mode="onSort" auto-generate="false">
73+
<igc-column field="OrderID" header="Order ID" merge="true"></igc-column>
74+
<igc-column field="ShipperName" header="Shipper Name" merge="false"></igc-column>
75+
<igc-column field="Salesperson" header="Salesperson"></igc-column>
76+
</{ComponentSelector}>
77+
```
6178
Here, the grid is set to merge only when columns are sorted, and both Category and Product columns are configured for merging.
6279

6380
## Custom Merge Conditions
6481
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.
6582

6683
### Merge Strategy Class
67-
A custom merge strategy must implement the `IgrGridMergeStrategy` class:
84+
A custom merge strategy must implement the `GridMergeStrategy` class:
6885

86+
<!-- React -->
6987
```ts
7088
export declare class IgrGridMergeStrategy {
7189
merge: (
@@ -80,14 +98,33 @@ export declare class IgrGridMergeStrategy {
8098
comparer: (prevRecord: any, record: any, field: string) => boolean;
8199
}
82100
```
101+
<!-- end: React -->
102+
<!-- WebComponents -->
103+
```ts
104+
export declare class IgcGridMergeStrategy {
105+
merge: (
106+
data: any[],
107+
field: string,
108+
comparer: (prevRecord: any, currentRecord: any, field: string) => boolean,
109+
result: any[],
110+
activeRowIndex?: number,
111+
grid?: GridType
112+
) => any[];
113+
114+
comparer: (prevRecord: any, record: any, field: string) => boolean;
115+
}
116+
```
117+
<!-- end: WebComponents -->
118+
83119
- `merge` - defines how merged cells are produced.
84120
- `comparer` - defines the condition to decide if two adjacent records should be merged.
85121

86122
<!-- ComponentStart: Grid, HierarchicalGrid -->
87123
### Extending the Default Strategy
88124

89-
If you only want to customize part of the behavior (for example, the comparer logic), you can extend the built-in `IgrDefaultMergeStrategy` and override the relevant methods.
125+
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.
90126

127+
<!-- React -->
91128
```ts
92129
export class MyCustomStrategy extends IgrDefaultMergeStrategy {
93130
/* Merge only cells within their respective projects */
@@ -100,6 +137,21 @@ export class MyCustomStrategy extends IgrDefaultMergeStrategy {
100137
}
101138
}
102139
```
140+
<!-- end: React -->
141+
<!-- WebComponents -->
142+
```ts
143+
export class MyCustomStrategy extends IgcDefaultMergeStrategy {
144+
/* Merge only cells within their respective projects */
145+
public override comparer(prevRecord: any, record: any, field: string): boolean {
146+
const a = prevRecord[field];
147+
const b = record[field];
148+
const projA = prevRecord['ProjectName'];
149+
const projB = record['ProjectName'];
150+
return a === b && projA === projB;
151+
}
152+
}
153+
```
154+
<!-- end: WebComponents -->
103155
<!-- ComponentEnd: Grid, HierarchicalGrid -->
104156
<!-- ComponentStart: TreeGrid -->
105157
The `IgxTreeGrid` provides two built-in strategies that implement the `IGridMergeStrategy` interface: `DefaultTreeGridMergeStrategy` and `ByLevelTreeGridMergeStrategy`. `DefaultTreeGridMergeStrategy` merges all cells with the same value, regardless of their hierarchical level. In contrast, `ByLevelTreeGridMergeStrategy` only merges cells if they have the same value and are located at the same level, making level a required condition for merging.
@@ -108,6 +160,7 @@ The `IgxTreeGrid` provides two built-in strategies that implement the `IGridMerg
108160

109161
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 `DefaultTreeGridMergeStrategy` or `ByLevelTreeGridMergeStrategy`, and override the relevant methods.
110162

163+
<!-- React -->
111164
```ts
112165
export class MyCustomStrategy extends IgrDefaultTreeGridMergeStrategy {
113166
/* Merge only cells within their respective projects */
@@ -120,10 +173,26 @@ export class MyCustomStrategy extends IgrDefaultTreeGridMergeStrategy {
120173
}
121174
}
122175
```
176+
<!-- end: React -->
177+
<!-- WebComponents -->
178+
```ts
179+
export class MyCustomStrategy extends IgcDefaultTreeGridMergeStrategy {
180+
/* Merge only cells within their respective projects */
181+
public override comparer(prevRecord: any, record: any, field: string): boolean {
182+
const a = prevRecord[field];
183+
const b = record[field];
184+
const projA = prevRecord['ProjectName'];
185+
const projB = record['ProjectName'];
186+
return a === b && projA === projB;
187+
}
188+
}
189+
```
190+
<!-- end: WebComponents -->
123191
<!-- ComponentEnd: TreeGrid -->
124192

125193
### Applying a Custom Strategy
126194
Once defined, assign the strategy to the grid through the `mergeStrategy` property:
195+
<!-- React -->
127196
```tsx
128197
<{ComponentSelector} data={data} mergeStrategy={customStrategy}>
129198
<IgrColumn field="ActionID" merge={true}></IgrColumn>
@@ -133,6 +202,18 @@ Once defined, assign the strategy to the grid through the `mergeStrategy` proper
133202
```ts
134203
const customStrategy = new MyCustomStrategy() as IgrGridMergeStrategy;
135204
```
205+
<!-- end: React -->
206+
<!-- WebComponents -->
207+
```ts
208+
constructor() {
209+
const grid = (this.grid = document.getElementById('grid') as IgcGridComponent);
210+
211+
grid.data = this.data;
212+
grid.mergeStrategy = new MyCustomStrategy() as IgcGridMergeStrategy;
213+
grid.cellMergeMode = 'always';
214+
}
215+
```
216+
<!-- end: WebComponents -->
136217
<!-- ComponentStart: Grid -->
137218
### Demo
138219
`sample="/{ComponentSample}/cell-merge-custom-sample", height="700", alt="{Platform} {ComponentTitle} Cell Merging Example"`

docfx/en/components/toc.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
"href": "grids/data-grid.md",
160160
"items": [
161161
{
162-
"exclude": ["Angular", "WebComponents", "Blazor"],
162+
"exclude": ["Angular", "Blazor"],
163163
"name": "Cell Merging",
164164
"href": "grids/grid/cell-merging.md",
165165
"status": "NEW"
@@ -403,7 +403,7 @@
403403
"href": "grids/hierarchical-grid/overview.md",
404404
"items": [
405405
{
406-
"exclude": ["Angular", "WebComponents", "Blazor"],
406+
"exclude": ["Angular", "Blazor"],
407407
"name": "Cell Merging",
408408
"href": "grids/hierarchical-grid/cell-merging.md",
409409
"status": "NEW"
@@ -631,7 +631,7 @@
631631
"href": "grids/tree-grid/overview.md",
632632
"items": [
633633
{
634-
"exclude": ["Angular", "WebComponents", "Blazor"],
634+
"exclude": ["Angular", "Blazor"],
635635
"name": "Cell Merging",
636636
"href": "grids/tree-grid/cell-merging.md",
637637
"status": "NEW"

0 commit comments

Comments
 (0)