Skip to content

Commit df1b339

Browse files
authored
Merge pull request #10285 from IgniteUI/mdragnev/pivot-formatting
Pivot grid formatting, cell classes, default data types.
2 parents fc42531 + e8cd0d5 commit df1b339

File tree

8 files changed

+212
-34
lines changed

8 files changed

+212
-34
lines changed

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
7070
/**
7171
* @hidden @internal
7272
*/
73-
@ViewChild('record_template', { read: TemplateRef, static: true })
74-
public recordTemplate: TemplateRef<any>;
73+
@ViewChild('record_template', { read: TemplateRef, static: true })
74+
public recordTemplate: TemplateRef<any>;
7575

7676
/**
7777
* @hidden @internal
@@ -157,7 +157,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
157157
* let data = this.grid.data;
158158
* ```
159159
*/
160-
public get data(): any[] | null {
160+
public get data(): any[] | null {
161161
return this._data;
162162
}
163163
/**
@@ -169,21 +169,21 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
169169
* }];
170170
* ```
171171
*/
172-
public set filteredData(value) {
173-
this._filteredData = value;
174-
}
172+
public set filteredData(value) {
173+
this._filteredData = value;
174+
}
175175

176-
/**
177-
* Returns an array of objects containing the filtered data.
178-
* ```typescript
179-
* let filteredData = this.grid.filteredData;
180-
* ```
181-
*
182-
* @memberof IgxHierarchicalGridComponent
183-
*/
184-
public get filteredData() {
185-
return this._filteredData;
186-
}
176+
/**
177+
* Returns an array of objects containing the filtered data.
178+
* ```typescript
179+
* let filteredData = this.grid.filteredData;
180+
* ```
181+
*
182+
* @memberof IgxHierarchicalGridComponent
183+
*/
184+
public get filteredData() {
185+
return this._filteredData;
186+
}
187187

188188

189189
/**
@@ -251,7 +251,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
251251
fieldChild.hidden = true;
252252
});
253253
}
254-
}
254+
}
255255

256256
/**
257257
* @hidden
@@ -267,7 +267,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
267267
/**
268268
* @hidden
269269
*/
270-
protected autogenerateColumns() {
270+
protected autogenerateColumns() {
271271
const data = this.gridAPI.get_data();
272272
const fieldsMap = PivotUtil.getFieldsHierarchy(
273273
data,
@@ -284,7 +284,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
284284
}
285285
}
286286

287-
protected generateColumnHierarchy(fields: Map<string, any>, data, parent = null): IgxColumnComponent[] {
287+
protected generateColumnHierarchy(fields: Map<string, any>, data, parent = null): IgxColumnComponent[] {
288288
const factoryColumn = this.resolver.resolveComponentFactory(IgxColumnComponent);
289289
const factoryColumnGroup = this.resolver.resolveComponentFactory(IgxColumnGroupComponent);
290290
let columns = [];
@@ -337,6 +337,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
337337
}
338338
}
339339
});
340+
this.reflow();
340341
return columns;
341342
}
342343

@@ -348,12 +349,11 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
348349
ref.instance.field = parent.field + '-' + val.member;
349350
ref.instance.parent = parent;
350351
ref.instance.hidden = hidden;
351-
ref.instance.dataType = this.resolveDataTypes(data[0][val.member]);
352+
ref.instance.dataType = val.dataType || this.resolveDataTypes(data[0][val.member]);
353+
ref.instance.formatter = val.formatter;
352354
ref.changeDetectorRef.detectChanges();
353355
cols.push(ref.instance);
354356
});
355357
return cols;
356358
}
357-
358-
359359
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export interface IPivotValue {
2525
aggregate: (data: any[]) => any;
2626
// Enables/Disables a particular value from pivot aggregation.
2727
enabled: boolean;
28+
// Allow conditionally styling of the IgxPivotGrid cells
29+
styles?: any;
30+
// Enables a data type specific template of the cells
31+
dataType?: string;
32+
// Applies display format to cell values.
33+
formatter?: (value: any, rowData?: any) => any;
2834
}
2935

3036
export interface IPivotKeys {
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { Component, ViewChild } from '@angular/core';
2+
import { fakeAsync, TestBed } from '@angular/core/testing';
3+
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
4+
import { IgxNumberSummaryOperand, IgxPivotGridComponent, IgxPivotGridModule, IPivotConfiguration } from 'igniteui-angular';
5+
import { configureTestSuite } from '../../test-utils/configure-suite';
6+
7+
describe('Basic IgxPivotGrid #pivotGrid', () => {
8+
let fixture;
9+
configureTestSuite((() => {
10+
TestBed.configureTestingModule({
11+
declarations: [
12+
IgxPivotGridTestBaseComponent
13+
],
14+
imports: [
15+
NoopAnimationsModule, IgxPivotGridModule]
16+
});
17+
}));
18+
19+
beforeEach(fakeAsync(() => {
20+
fixture = TestBed.createComponent(IgxPivotGridTestBaseComponent);
21+
fixture.detectChanges();
22+
}));
23+
24+
it('should apply formatter and dataType from measures', () => {
25+
fixture.detectChanges();
26+
const pivotGrid = fixture.componentInstance.pivotGrid;
27+
const actualFormatterValue = pivotGrid.rowList.first.cells.first.title;
28+
expect(actualFormatterValue).toEqual('774$');
29+
const actualDataTypeValue = pivotGrid.rowList.first.cells.last.title;
30+
expect(actualDataTypeValue).toEqual('$71.89');
31+
});
32+
33+
it('should apply css class to cells from measures', () => {
34+
fixture.detectChanges();
35+
const pivotGrid = fixture.componentInstance.pivotGrid;
36+
const cells = pivotGrid.rowList.first.cells;
37+
expect(cells.first.nativeElement.classList).toContain('test');
38+
expect(cells.last.nativeElement.classList).not.toContain('test');
39+
});
40+
});
41+
42+
43+
@Component({
44+
template: `
45+
<igx-pivot-grid #grid [data]="data" [pivotConfiguration]="pivotConfigHierarchy">
46+
</igx-pivot-grid>`
47+
})
48+
export class IgxPivotGridTestBaseComponent {
49+
@ViewChild('grid', { read: IgxPivotGridComponent, static: true }) public pivotGrid: IgxPivotGridComponent;
50+
public data;
51+
52+
53+
public cellClasses;
54+
55+
56+
public pivotConfigHierarchy: IPivotConfiguration;
57+
58+
constructor() {
59+
this.data = [
60+
{
61+
ProductCategory: 'Clothing', UnitPrice: 12.81, SellerName: 'Stanley',
62+
Country: 'Bulgaria', Date: '01/01/2021', UnitsSold: 282
63+
},
64+
{
65+
ProductCategory: 'Clothing', UnitPrice: 49.57, SellerName: 'Elisa',
66+
Country: 'USA', Date: '01/05/2019', UnitsSold: 296
67+
},
68+
{
69+
ProductCategory: 'Bikes', UnitPrice: 3.56, SellerName: 'Lydia',
70+
Country: 'Uruguay', Date: '01/06/2020', UnitsSold: 68
71+
},
72+
{
73+
ProductCategory: 'Accessories', UnitPrice: 85.58, SellerName: 'David',
74+
Country: 'USA', Date: '04/07/2021', UnitsSold: 293
75+
},
76+
{
77+
ProductCategory: 'Components', UnitPrice: 18.13, SellerName: 'John',
78+
Country: 'USA', Date: '12/08/2021', UnitsSold: 240
79+
},
80+
{
81+
ProductCategory: 'Clothing', UnitPrice: 68.33, SellerName: 'Larry',
82+
Country: 'Uruguay', Date: '05/12/2020', UnitsSold: 456
83+
},
84+
{
85+
ProductCategory: 'Clothing', UnitPrice: 16.05, SellerName: 'Walter',
86+
Country: 'Bulgaria', Date: '02/19/2020', UnitsSold: 492
87+
}];
88+
89+
this.cellClasses = {
90+
test: this.callback,
91+
test2: this.callback1
92+
};
93+
94+
this.pivotConfigHierarchy = {
95+
columns: [{
96+
member: 'Country',
97+
enabled: true,
98+
childLevels: []
99+
},
100+
],
101+
rows: [{
102+
member: () => 'All',
103+
enabled: true,
104+
childLevels: [
105+
{
106+
fieldName: 'ProductCategory',
107+
member: (data) => data.ProductCategory,
108+
enabled: true,
109+
childLevels: []
110+
}
111+
]
112+
}],
113+
values: [
114+
{
115+
member: 'UnitsSold',
116+
aggregate: IgxNumberSummaryOperand.sum,
117+
enabled: true,
118+
// dataType: 'currency',
119+
formatter: (value) => value ? value + '$' : undefined,
120+
styles: this.cellClasses
121+
},
122+
{
123+
member: 'UnitPrice',
124+
aggregate: IgxNumberSummaryOperand.sum,
125+
enabled: true,
126+
dataType: 'currency'
127+
}
128+
],
129+
filters: null
130+
};
131+
}
132+
133+
public callback = (rowData: any, columnKey: any) => rowData[columnKey] >= 5;
134+
public callback1 = (rowData: any, columnKey: any) => rowData[columnKey] < 5;
135+
136+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020

2121
<ng-template igxGridFor let-col [igxGridForOf]="unpinnedColumns | igxNotGrouped" [igxForScrollContainer]="grid.parentVirtDir" let-colIndex="index" [igxForSizePropName]='"calcPixelWidth"' [igxForScrollOrientation]="'horizontal'" [igxForContainerSize]='grid.unpinnedWidth' [igxForTrackBy]='grid.trackColumnChanges' #igxDirRef>
2222
<igx-grid-cell
23+
#cell
2324
class="igx-grid__td igx-grid__td--fw"
2425
[class.igx-grid__td--edited]="rowID | transactionState:col.field:grid.rowEditable:grid.transactions:grid.pipeTrigger:grid.gridAPI.crudService.cell:grid.gridAPI.crudService.row"
2526
[attr.aria-describedby]="gridID + '_' + col.field | igxStringReplace:'.':'_'"
2627
[class.igx-grid__td--number]="col.dataType === 'number' || col.dataType === 'percent' || col.dataType === 'currency'"
2728
[class.igx-grid__td--bool]="col.dataType === 'boolean'"
28-
[ngClass]="col.cellClasses | igxCellStyleClasses:rowData[col.field]:rowData:col.field:viewIndex:grid.pipeTrigger"
29+
[ngClass]="this.getCellClass(col) | igxCellStyleClasses:rowData[col.field]:rowData:col.field:viewIndex:grid.pipeTrigger"
2930
[ngStyle]="col.cellStyles | igxCellStyles:rowData[col.field]:rowData:col.field:viewIndex:grid.pipeTrigger"
3031
[editMode]="col.editable && this.grid.crudService.targetInEdit(index, col.index)"
3132
[column]="col"
@@ -105,4 +106,4 @@
105106
{{ expandState ? 'expand_more' : 'expand_less'}}</igx-icon>
106107
{{column.header}}
107108
</div>
108-
</ng-template>
109+
</ng-template>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export class IgxPivotRowComponent extends IgxRowDirective<IgxPivotGridComponent>
6969
return this.grid.gridAPI.get_row_expansion_state(this.rowDimensionKey);
7070
}
7171

72+
7273
/**
7374
* @hidden
7475
* @internal
@@ -84,6 +85,12 @@ export class IgxPivotRowComponent extends IgxRowDirective<IgxPivotGridComponent>
8485
}
8586
}
8687

88+
public getCellClass(col: any) {
89+
const colName = col.field.split('-');
90+
const measureName = colName[colName.length - 1];
91+
return this.grid.pivotConfiguration.values.find(v => v.member === measureName)?.styles;
92+
}
93+
8794
protected extractFromDimensions(rowDimConfig: IPivotDimension[], level: number) {
8895
let dimIndex = 0;
8996
for (const dim of rowDimConfig) {

src/app/pivot-grid/pivot-grid.sample.css

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
:host ::ng-deep {
2+
.upFont {
3+
color: green;
4+
}
5+
6+
.downFont {
7+
color:red;
8+
}
9+
10+
.upFont1 {
11+
color: blue;
12+
}
13+
14+
.downFont1 {
15+
color:yellow;
16+
}
17+
18+
.sample-column {
19+
max-width: 900px;
20+
}
21+
}

src/app/pivot-grid/pivot-grid.sample.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { HIERARCHICAL_SAMPLE_DATA } from '../shared/sample-data';
55
@Component({
66
providers: [],
77
selector: 'app-tree-grid-sample',
8-
styleUrls: ['pivot-grid.sample.css'],
8+
styleUrls: ['pivot-grid.sample.scss'],
99
templateUrl: 'pivot-grid.sample.html'
1010
})
1111

@@ -56,17 +56,16 @@ export class PivotGridSampleComponent {
5656
enabled: true,
5757
childLevels: []
5858
},
59-
]
59+
]
6060
}]
6161
},
62-
6362
{
6463
member: 'ProductCategory',
6564
enabled: true,
6665
childLevels: []
6766
}
6867
]
69-
,
68+
,
7069
rows: [{
7170
member: 'ProductCategory',
7271
enabled: true,
@@ -81,12 +80,23 @@ export class PivotGridSampleComponent {
8180
{
8281
member: 'UnitsSold',
8382
aggregate: IgxNumberSummaryOperand.sum,
84-
enabled: true
83+
enabled: true,
84+
styles: {
85+
upFont: (rowData: any, columnKey: any): boolean => rowData[columnKey] > 300,
86+
downFont: (rowData: any, columnKey: any): boolean => rowData[columnKey] <= 300
87+
},
88+
// dataType: 'currency',
89+
formatter: (value) => value ? value + '$' : undefined
8590
},
8691
{
8792
member: 'UnitPrice',
8893
aggregate: IgxNumberSummaryOperand.sum,
89-
enabled: true
94+
enabled: true,
95+
dataType: 'currency',
96+
styles: {
97+
upFont1: (rowData: any, columnKey: any): boolean => rowData[columnKey] > 50,
98+
downFont1: (rowData: any, columnKey: any): boolean => rowData[columnKey] <= 50
99+
},
90100
}
91101
],
92102
filters: null

0 commit comments

Comments
 (0)