Skip to content

Commit b2f08b3

Browse files
ChronosSFdkamburov
andauthored
feat(groupby): adding cell-like formatting for group values #12752 (#12768)
* feat(groupby): adding cell-like formatting for group values #12752 * chore(groupby): adding changelog, fixing lint * fix(groupby): removing leftovers from the tempalte * chore(groupby): fixing an issue with non-existing columns --------- Co-authored-by: Deyan Kamburov <[email protected]>
1 parent cc7e4a9 commit b2f08b3

File tree

4 files changed

+62
-17
lines changed

4 files changed

+62
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All notable changes for each version of this project will be documented in this
1919
- The `IgxPivotDateDimension` properties `inBaseDimension` and `inOption` have been deprecated and renamed to `baseDimension` and `options` respectively.
2020
- `IgxGrid`
2121
- **Breaking Change** The `onGroupingDone` output has been renamed to `groupingDone` to not violate the no on-prefixed outputs convention. Automatic migrations are available and will be applied on `ng update`.
22+
- Column formatters are now applied to values shown group rows when using the default template. For custom formatters, the formatter function is called with the data from the first row in the group.
2223
- `DisplayDensity`
2324
- **Breaking Change** The `onDensityChanged` output has been renamed to `densityChanged` to not violate the no on-prefixed outputs convention. All components exposing this event are affected. Automatic migrations are available and will be applied on `ng update`.
2425
- `IgxGrid`, `IgxTreeGrid`, `IgxHierarchicalGrid`

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, ViewChild, TemplateRef, QueryList } from '@angular/core';
2+
import { formatNumber } from '@angular/common'
23
import { fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
34
import { By } from '@angular/platform-browser';
45
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
@@ -788,7 +789,7 @@ describe('IgxGrid - GroupBy #grid', () => {
788789
fix.detectChanges();
789790
grid.groupBy({
790791
fieldName: 'ReleaseDate', dir: SortingDirection.Asc, ignoreCase: false
791-
});
792+
});
792793
fix.detectChanges();
793794
spyOn(grid.groupingExpressionsChange, 'emit');
794795
fix.detectChanges();
@@ -1516,7 +1517,7 @@ describe('IgxGrid - GroupBy #grid', () => {
15161517
expect(GridSelectionFunctions.verifyGroupByRowCheckboxState(grRow, true, false));
15171518
}));
15181519

1519-
it('the group row selector state should be indeterminated if some of the records in the group but not all are selected',
1520+
it('the group row selector state should be undetermined if some of the records in the group but not all are selected',
15201521
fakeAsync(() => {
15211522
const fix = TestBed.createComponent(DefaultGridComponent);
15221523
const grid = fix.componentInstance.instance;
@@ -2088,6 +2089,33 @@ describe('IgxGrid - GroupBy #grid', () => {
20882089
expect(fix.componentInstance.onGroupByRowClick).toHaveBeenCalledWith(fix.componentInstance.groupByRowClick, contextUnselect);
20892090
}));
20902091

2092+
// GroupBy Row Formatting
2093+
it('should properly apply formatters, both custom and default ones for the default row value template', fakeAsync(() => {
2094+
const fix = TestBed.createComponent(GroupableGridComponent);
2095+
const grid = fix.componentInstance.instance;
2096+
tick();
2097+
fix.detectChanges();
2098+
grid.groupBy({
2099+
fieldName: 'Downloads', dir: SortingDirection.Desc, ignoreCase: false
2100+
});
2101+
tick();
2102+
fix.detectChanges();
2103+
let cellText = grid.groupsRowList.first.nativeElement.querySelector(".igx-group-label__text").innerText;
2104+
expect(cellText).toEqual(formatNumber(1000, grid.locale));
2105+
// apply custom formatter
2106+
grid.getColumnByName('Downloads').formatter = (value, _row) => `\$${value}`;
2107+
grid.groupingExpressions = [];
2108+
tick();
2109+
fix.detectChanges();
2110+
grid.groupBy({
2111+
fieldName: 'Downloads', dir: SortingDirection.Desc, ignoreCase: false
2112+
});
2113+
tick();
2114+
fix.detectChanges();
2115+
cellText = grid.groupsRowList.first.nativeElement.querySelector(".igx-group-label__text").innerText;
2116+
expect(cellText).toEqual('$1000');
2117+
}));
2118+
20912119
// GroupBy + Resizing
20922120
it('should retain same size for group row after a column is resized.', fakeAsync(() => {
20932121
const fix = TestBed.createComponent(DefaultGridComponent);
@@ -3819,7 +3847,7 @@ export class CustomTemplateGridComponent extends DataParent {
38193847
public width = '800px';
38203848
public height = null;
38213849

3822-
@ViewChild('template', {read: TemplateRef })
3850+
@ViewChild('template', { read: TemplateRef })
38233851
public customGroupBy: TemplateRef<any>;
38243852
}
38253853

projects/igniteui-angular/src/lib/grids/grid/groupby-row.component.html

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@
4848
(groupRow.expression ? groupRow.expression.fieldName : '') }}:
4949
</span>
5050

51-
<ng-container *ngIf="dataType === 'boolean' || dataType === 'string'; else default">
52-
<span class="igx-group-label__text">{{ groupRow.value }}</span>
53-
</ng-container>
54-
<ng-template #default>
55-
<ng-container *ngIf="dataType === 'number'">
56-
<span class="igx-group-label__text">{{ groupRow.value | number }}</span>
57-
</ng-container>
58-
<ng-container *ngIf="dataType === 'date' || dataType === 'dateTime' || dataType === 'time'">
59-
<span class="igx-group-label__text">{{ groupRow.value |
60-
date:groupRow.column.pipeArgs.format:groupRow.column.pipeArgs.timezone:grid.locale }}</span>
61-
</ng-container>
62-
</ng-template>
51+
<span class="igx-group-label__text">{{
52+
formatter
53+
? (groupRow.value | columnFormatter:formatter:groupRow.records[0]:null)
54+
: dataType === "number"
55+
? (groupRow.value | number:groupRow.column.pipeArgs.digitsInfo:grid.locale)
56+
: (dataType === 'date' || dataType === 'time' || dataType === 'dateTime')
57+
? (groupRow.value | date:groupRow.column.pipeArgs.format:groupRow.column.pipeArgs.timezone:grid.locale)
58+
: dataType === 'currency'
59+
? (groupRow.value | currency:currencyCode:groupRow.column.pipeArgs.display:groupRow.column.pipeArgs.digitsInfo:grid.locale)
60+
: dataType === 'percent'
61+
? (groupRow.value | percent:groupRow.column.pipeArgs.digitsInfo:grid.locale)
62+
: groupRow.value
63+
}}</span>
6364

6465
<igx-badge [value]="groupRow.records ? groupRow.records.length : 0" class='igx-group-label__count-badge'>
6566
</igx-badge>

projects/igniteui-angular/src/lib/grids/grid/groupby-row.component.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Subject } from 'rxjs';
2121
import { IgxGridRowComponent } from './grid-row.component';
2222
import { GridSelectionMode } from '../common/enums';
2323
import { ISelectionNode } from '../common/types';
24+
import { getLocaleCurrencyCode } from '@angular/common';
2425

2526
@Component({
2627
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -119,6 +120,12 @@ export class IgxGridGroupByRowComponent implements OnDestroy {
119120
return this.isActive();
120121
}
121122

123+
/** @hidden @internal */
124+
public get currencyCode(): string {
125+
return this.groupRow.column.pipeArgs.currencyCode ?
126+
this.groupRow.column.pipeArgs.currencyCode : getLocaleCurrencyCode(this.grid.locale);
127+
}
128+
122129
constructor(
123130
@Inject(IGX_GRID_BASE) public grid: GridType,
124131
public gridSelection: IgxGridSelectionService,
@@ -249,13 +256,21 @@ export class IgxGridGroupByRowComponent implements OnDestroy {
249256
}
250257

251258
/**
252-
* @hidden
253-
*/
259+
* @hidden @internal
260+
*/
254261
public get dataType(): any {
255262
const column = this.groupRow.column;
256263
return (column && column.dataType) || GridColumnDataType.String;
257264
}
258265

266+
/**
267+
* @hidden @internal
268+
*/
269+
public get formatter(): any {
270+
const column = this.groupRow.column;
271+
return (column && column.formatter) || null;
272+
}
273+
259274
/**
260275
* @hidden @internal
261276
*/

0 commit comments

Comments
 (0)