-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.component.ts
More file actions
135 lines (116 loc) · 3.58 KB
/
app.component.ts
File metadata and controls
135 lines (116 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { Component, ViewChild } from '@angular/core';
import {
DxDataGridComponent,
type DxDataGridTypes,
} from 'devextreme-angular/ui/data-grid';
import type { DxPivotGridTypes } from 'devextreme-angular/ui/pivot-grid';
import PivotGridDataSource from 'devextreme/ui/pivot_grid/data_source';
import {
ArrayStore, DataSource, isItemsArray, LoadResult,
} from 'devextreme-angular/common/data';
import notify from 'devextreme/ui/notify';
import { Sale, Service } from './app.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
@ViewChild('drillDownDataGrid', { static: false })
drillDownDataGrid!: DxDataGridComponent<Sale, number>;
data: ArrayStore;
pivotGridDataSource: PivotGridDataSource;
drillDownDataSource: DataSource<Sale, number> | null = null;
dataGridDataSource: DataSource<Sale, number> | null = null;
popupVisible = false;
popupTitle = '';
constructor(service: Service) {
this.data = new ArrayStore({
data: service.getSales(),
key: 'id',
});
this.pivotGridDataSource = new PivotGridDataSource({
fields: [
{
caption: 'Region',
width: 120,
dataField: 'region',
area: 'row',
},
{
caption: 'City',
dataField: 'city',
width: 150,
area: 'row',
selector: this.citySelector,
},
{
dataField: 'date',
dataType: 'date',
area: 'column',
},
{
caption: 'Sales',
dataField: 'amount',
dataType: 'number',
summaryType: 'sum',
format: 'currency',
area: 'data',
},
],
store: this.data,
});
}
citySelector(data: Sale): string {
return `${data.city} (${data.country})`;
}
onCellClick(e: DxPivotGridTypes.CellClickEvent): void {
if (e.area === 'data' && e.cell && e.cell.rowPath) {
const rowPathLength = e.cell.rowPath.length;
const rowPathName = e.cell.rowPath[rowPathLength - 1];
const popupTitle = `${rowPathName || 'Total'} Drill Down Data`;
this.drillDownDataSource = this.pivotGridDataSource.createDrillDownDataSource(e.cell);
this.popupTitle = popupTitle;
this.popupVisible = true;
}
}
onPopupShowing(): void {
this.drillDownDataSource?.store().load().then((items: LoadResult<Sale>) => {
if (isItemsArray(items)) {
this.dataGridDataSource = new DataSource<Sale, number>({
store: new ArrayStore({
key: this.data.key(),
data: items,
}),
});
}
}).catch(() => {
notify('Failed to load drill-down data', 'error', 1000);
});
}
onPopupHiding(): void {
this.pivotGridDataSource.reload().catch(() => {
notify('Failed to reload data', 'error', 1000);
});
}
onPopupShown(): void {
const gridInstance = this.drillDownDataGrid?.instance;
if (!gridInstance) return;
gridInstance.updateDimensions();
}
onRowUpdating(e: DxDataGridTypes.RowUpdatingEvent<Sale, number>): void {
this.data.update(e.key, e.newData).catch(() => {
notify('Failed to update data', 'error', 1000);
});
}
onRowAdding(e: DxDataGridTypes.RowInsertingEvent<Sale, number>): void {
this.data.insert(e.data).catch(() => {
notify('Failed to insert data', 'error', 1000);
});
}
onRowRemoving(e: DxDataGridTypes.RowRemovingEvent<Sale, number>): void {
this.data.remove(e.key).catch(() => {
notify('Failed to remove data', 'error', 1000);
});
}
}