|
1 | | -import { Component } from '@angular/core'; |
2 | | -import { ClickEvent } from 'devextreme/ui/button'; |
| 1 | +import { Component, ViewChild } from '@angular/core'; |
| 2 | +import { DxCheckBoxModule, DxPivotGridComponent, DxPivotGridModule } from 'devextreme-angular'; |
| 3 | +import PivotGridDataSource from 'devextreme/ui/pivot_grid/data_source'; |
| 4 | +import { Sale, Service } from './app.service'; |
| 5 | + |
| 6 | +interface CustomSummaryValue { |
| 7 | + summaryProcess?: string; |
| 8 | + value?: any; |
| 9 | + totalValue?: any; |
| 10 | +} |
3 | 11 |
|
4 | 12 | @Component({ |
5 | 13 | selector: 'app-root', |
6 | 14 | templateUrl: './app.component.html', |
7 | 15 | styleUrls: ['./app.component.scss'], |
| 16 | + standalone: true, |
| 17 | + imports: [DxPivotGridModule, DxCheckBoxModule], |
| 18 | + providers: [Service], |
8 | 19 | }) |
9 | 20 | export class AppComponent { |
10 | | - title = 'Angular'; |
| 21 | + @ViewChild('sales') pivotGrid!: DxPivotGridComponent; |
| 22 | + |
| 23 | + sales: Sale[]; |
| 24 | + |
| 25 | + dataSource: PivotGridDataSource; |
| 26 | + |
| 27 | + isConditional = true; |
11 | 28 |
|
12 | | - counter = 0; |
| 29 | + constructor(service: Service) { |
| 30 | + let tempSales: any = service.getSales(); |
| 31 | + |
| 32 | + tempSales.forEach((sale: Sale, index: number) => { |
| 33 | + sale.isApproved = index % 2 !== 0; |
| 34 | + }); |
| 35 | + |
| 36 | + this.sales = tempSales; |
| 37 | + |
| 38 | + this.dataSource = new PivotGridDataSource({ |
| 39 | + fields: [{ |
| 40 | + caption: 'Region', |
| 41 | + width: 120, |
| 42 | + dataField: 'region', |
| 43 | + area: 'row', |
| 44 | + expanded: true, |
| 45 | + }, { |
| 46 | + caption: 'City', |
| 47 | + dataField: 'city', |
| 48 | + width: 150, |
| 49 | + area: 'row', |
| 50 | + selector: (data: Sale): string => `${data.city} (${data.country})`, |
| 51 | + }, { |
| 52 | + dataField: 'date', |
| 53 | + dataType: 'date', |
| 54 | + area: 'column', |
| 55 | + expanded: true, |
| 56 | + }, { |
| 57 | + caption: 'Sales', |
| 58 | + dataType: 'number', |
| 59 | + summaryType: 'custom', |
| 60 | + format: 'currency', |
| 61 | + area: 'data', |
| 62 | + calculateCustomSummary: this.calculateCustomSummary, |
| 63 | + }, |
| 64 | + { |
| 65 | + caption: 'Approved', |
| 66 | + dataField: 'isApproved', |
| 67 | + summaryType: 'sum', |
| 68 | + area: 'data', |
| 69 | + }], |
| 70 | + store: this.sales, |
| 71 | + }); |
| 72 | + } |
13 | 73 |
|
14 | | - buttonText = 'Click count: 0'; |
| 74 | + calculateCustomSummary = (options: CustomSummaryValue): void => { |
| 75 | + switch (options.summaryProcess) { |
| 76 | + case 'start': |
| 77 | + options.totalValue = { conditionalVal: 0, rawVal: 0, count: 0 }; |
| 78 | + break; |
| 79 | + case 'calculate': |
| 80 | + options.totalValue.count += 1; |
| 81 | + options.totalValue.rawVal += options.value.amount; |
| 82 | + if (options.value.isApproved) { |
| 83 | + options.totalValue.conditionalVal += options.value.amount; |
| 84 | + } |
| 85 | + break; |
| 86 | + case 'finalize': |
| 87 | + if (options.totalValue.count === 1 || !this.isConditional) { |
| 88 | + options.totalValue = options.totalValue.rawVal; |
| 89 | + } else { |
| 90 | + options.totalValue = options.totalValue.conditionalVal; |
| 91 | + } |
| 92 | + break; |
| 93 | + default: break; |
| 94 | + } |
| 95 | + }; |
15 | 96 |
|
16 | | - onClick(e: ClickEvent): void { |
17 | | - this.counter++; |
18 | | - this.buttonText = `Click count: ${this.counter}`; |
| 97 | + toggleConditionalChanged(): void { |
| 98 | + this.pivotGrid.instance.getDataSource().reload().catch(() => {}); |
19 | 99 | } |
20 | 100 | } |
0 commit comments