Skip to content

Commit 64a7260

Browse files
feat(grid-summary): summary disabling with disabledSummaries property
1 parent 811d5a8 commit 64a7260

File tree

5 files changed

+172
-2
lines changed

5 files changed

+172
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes for each version of this project will be documented in this file.
44

5+
## 19.0.0
6+
### New Features
7+
- `IgxColumn`
8+
- Introduced the `disabledSummaries` property, allowing users to specify which summaries should be disabled for a given column. This property accepts an array of strings corresponding to the summary keys, enabling selective control over both default summaries (e.g., 'Count', 'Min') and any custom summaries created by the user.
9+
510
## 18.2.0
611
### General
712
- `IFilteringExpressionsTree`, `FilteringExpressionsTree`

projects/igniteui-angular/src/lib/grids/columns/column.component.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,33 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
10771077
this.grid.summaryService.resetSummaryHeight();
10781078
}
10791079
}
1080+
1081+
/**
1082+
* Sets/gets the summary operands to exclude from display.
1083+
* Accepts an array of string keys representing the summary types to disable, such as 'Min', 'Max', 'Count' etc.
1084+
* ```typescript
1085+
* let disabledSummaries = this.column.disabledSummaries;
1086+
* ```
1087+
* ```html
1088+
* <igx-column [disabledSummaries]="['min', 'max', 'average']"></igx-column>
1089+
* ```
1090+
*
1091+
* @memberof IgxColumnComponent
1092+
*/
1093+
@Input()
1094+
public get disabledSummaries(): string[] {
1095+
return this._disabledSummaries;
1096+
}
1097+
1098+
public set disabledSummaries(value: string[]) {
1099+
this._disabledSummaries = value;
1100+
if (this.grid) {
1101+
this.grid.summaryService.removeSummariesCachePerColumn(this.field);
1102+
this.grid.summaryPipeTrigger++;
1103+
this.grid.summaryService.resetSummaryHeight();
1104+
}
1105+
}
1106+
10801107
/**
10811108
* Gets the column `filters`.
10821109
* ```typescript
@@ -1743,6 +1770,10 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
17431770
* @hidden
17441771
*/
17451772
protected _summaries = null;
1773+
/**
1774+
* @hidden
1775+
*/
1776+
private _disabledSummaries: string[] = [];
17461777
/**
17471778
* @hidden
17481779
*/

projects/igniteui-angular/src/lib/grids/common/grid.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ export interface ColumnType extends FieldType {
443443
filteringExpressionsTree: FilteringExpressionsTree;
444444
hasSummary: boolean;
445445
summaries: any;
446+
disabledSummaries?: string[];
446447
/**
447448
* The template reference for a summary of the column
448449
* It is of type TemplateRef, which represents an embedded template, used to instantiate embedded views

projects/igniteui-angular/src/lib/grids/grid/grid-summary.spec.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,77 @@ describe('IgxGrid - Summaries #grid', () => {
134134
fixture.detectChanges();
135135
}).not.toThrow();
136136
});
137+
138+
it('should not display initially disabled summaries in the summary output', fakeAsync(() => {
139+
grid.enableSummaries([{ fieldName: 'UnitsInStock' }]);
140+
fixture.detectChanges();
141+
tick();
142+
143+
const column = grid.getColumnByName('UnitsInStock');
144+
145+
column.disabledSummaries = ['count', 'min', 'max'];
146+
fixture.detectChanges();
147+
tick();
148+
149+
GridSummaryFunctions.verifyColumnSummaries(
150+
GridSummaryFunctions.getRootSummaryRow(fixture), 3,
151+
['Sum', 'Avg'],
152+
['39,004', '3,900.4']
153+
);
154+
}));
155+
156+
it('should apply disabled summaries dynamically at runtime', fakeAsync(() => {
157+
grid.enableSummaries([{ fieldName: 'UnitsInStock' }]);
158+
fixture.detectChanges();
159+
tick();
160+
161+
const column = grid.getColumnByName('UnitsInStock');
162+
163+
column.disabledSummaries = [];
164+
fixture.detectChanges();
165+
tick();
166+
GridSummaryFunctions.verifyColumnSummaries(
167+
GridSummaryFunctions.getRootSummaryRow(fixture), 3,
168+
['Count', 'Min', 'Max', 'Sum', 'Avg'],
169+
['10', '0', '20,000', '39,004', '3,900.4']
170+
);
171+
172+
column.disabledSummaries = ['count'];
173+
fixture.detectChanges();
174+
tick();
175+
GridSummaryFunctions.verifyColumnSummaries(
176+
GridSummaryFunctions.getRootSummaryRow(fixture), 3,
177+
['Min', 'Max', 'Sum', 'Avg'],
178+
['0', '20,000', '39,004', '3,900.4']
179+
);
180+
181+
column.disabledSummaries = ['count', 'sum'];
182+
fixture.detectChanges();
183+
tick();
184+
GridSummaryFunctions.verifyColumnSummaries(
185+
GridSummaryFunctions.getRootSummaryRow(fixture), 3,
186+
['Min', 'Max', 'Avg'],
187+
['0', '20,000', '3,900.4']
188+
);
189+
190+
column.disabledSummaries = ['count', 'sum', 'average'];
191+
fixture.detectChanges();
192+
tick();
193+
GridSummaryFunctions.verifyColumnSummaries(
194+
GridSummaryFunctions.getRootSummaryRow(fixture), 3,
195+
['Min', 'Max'],
196+
['0', '20,000']
197+
);
198+
199+
column.disabledSummaries = ['min', 'max'];
200+
fixture.detectChanges();
201+
tick();
202+
GridSummaryFunctions.verifyColumnSummaries(
203+
GridSummaryFunctions.getRootSummaryRow(fixture), 3,
204+
['Count', 'Sum', 'Avg'],
205+
['10', '39,004', '3,900.4']
206+
);
207+
}));
137208
});
138209

139210
describe('custom summaries: ', () => {
@@ -238,6 +309,33 @@ describe('IgxGrid - Summaries #grid', () => {
238309
expect(lastColumnSummaryCellRect.right).toBe(lastColumnNormalCellRect.right,
239310
'summary cell and data cell are not right aligned');
240311
});
312+
313+
it('should apply disabledSummaries with custom summary', fakeAsync(() => {
314+
grid.enableSummaries([{ fieldName: 'UnitsInStock' }]);
315+
fixture.detectChanges();
316+
tick();
317+
318+
const column = grid.getColumnByName('UnitsInStock');
319+
column.summaries = fixture.componentInstance.inStockSummary;
320+
fixture.detectChanges();
321+
tick();
322+
323+
GridSummaryFunctions.verifyColumnSummaries(
324+
GridSummaryFunctions.getRootSummaryRow(fixture), 3,
325+
['Count', 'Min', 'Max', 'Sum', 'Avg', 'Items InStock'],
326+
['10', '0', '20,000', '39,004', '3,900.4', '6']
327+
);
328+
329+
column.disabledSummaries = ['test'];
330+
fixture.detectChanges();
331+
tick();
332+
333+
GridSummaryFunctions.verifyColumnSummaries(
334+
GridSummaryFunctions.getRootSummaryRow(fixture), 3,
335+
['Count', 'Min', 'Max', 'Sum', 'Avg'],
336+
['10', '0', '20,000', '39,004', '3,900.4']
337+
);
338+
}));
241339
});
242340

243341
describe('specific data: ', () => {

projects/igniteui-angular/src/lib/grids/summaries/grid-summary.service.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,38 @@ export class IgxGridSummaryService {
116116
rowSummaries = new Map<string, IgxSummaryResult[]>();
117117
this.summaryCacheMap.set(rowID, rowSummaries);
118118
}
119+
119120
if (!this.hasSummarizedColumns || !data) {
120121
return rowSummaries;
121122
}
123+
122124
this.grid.columns.filter(col => col.hasSummary).forEach((column) => {
123125
if (!rowSummaries.get(column.field)) {
124-
const summaryResult = column.summaries.operate(data.map(r => resolveNestedPath(r, column.field)),
125-
data, column.field, groupRecord, this.grid.locale, column.pipeArgs);
126+
let summaryResult = column.disabledSummaries.length
127+
? this.getCachedSummary(column.field, column.disabledSummaries) || null
128+
: null;
129+
130+
if (!summaryResult) {
131+
summaryResult = column.summaries.operate(
132+
data.map(r => resolveNestedPath(r, column.field)),
133+
data,
134+
column.field,
135+
groupRecord,
136+
this.grid.locale,
137+
column.pipeArgs
138+
);
139+
140+
if (column.disabledSummaries.length) {
141+
summaryResult = summaryResult.filter(
142+
result => !column.disabledSummaries.includes(result.key)
143+
);
144+
this.cacheSummary(column.field, summaryResult, column.disabledSummaries);
145+
}
146+
}
126147
rowSummaries.set(column.field, summaryResult);
127148
}
128149
});
150+
129151
return rowSummaries;
130152
}
131153

@@ -244,4 +266,17 @@ export class IgxGridSummaryService {
244266
});
245267
}
246268
}
269+
270+
private cacheSummary(field: string, summaryResult: IgxSummaryResult[], disabledSummaries: string[] = []) {
271+
if (!this.summaryCacheMap.has(field)) {
272+
this.summaryCacheMap.set(field, new Map<string, IgxSummaryResult[]>());
273+
}
274+
const cacheKey = JSON.stringify(disabledSummaries);
275+
this.summaryCacheMap.get(field).set(cacheKey, summaryResult);
276+
}
277+
278+
private getCachedSummary(field: string, disabledSummaries: string[] = []): IgxSummaryResult[] | undefined {
279+
const cacheKey = JSON.stringify(disabledSummaries);
280+
return this.summaryCacheMap.get(field)?.get(cacheKey);
281+
}
247282
}

0 commit comments

Comments
 (0)