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