|
1 | | -import { useCallback, useState } from 'react'; |
2 | | -import './App.css'; |
3 | 1 | import 'devextreme/dist/css/dx.material.blue.light.compact.css'; |
4 | | -import Button from 'devextreme-react/button'; |
| 2 | +import { |
| 3 | + useMemo, useState, useRef, useCallback, |
| 4 | +} from 'react'; |
| 5 | +import { type DataSourceOptions } from 'devextreme/common/data'; |
| 6 | +import PivotGrid, { FieldChooser, type PivotGridRef } from 'devextreme-react/pivot-grid'; |
| 7 | +import CheckBox, { type CheckBoxTypes } from 'devextreme-react/check-box'; |
| 8 | +import service, { type Sale } from './service.ts'; |
| 9 | +import './App.css'; |
| 10 | + |
| 11 | +interface CustomSummaryValue { |
| 12 | + summaryProcess?: string; |
| 13 | + value?: any; |
| 14 | + totalValue?: any; |
| 15 | +} |
| 16 | + |
| 17 | +const tempSales = service.getSales(); |
| 18 | + |
| 19 | +tempSales.forEach((sale: Sale, index: number) => { |
| 20 | + sale.isApproved = index % 2 !== 0; |
| 21 | +}); |
| 22 | + |
| 23 | +const sales = tempSales; |
| 24 | + |
| 25 | +function citySelector(data: Sale): string { |
| 26 | + return `${data.city} (${data.country})`; |
| 27 | +} |
5 | 28 |
|
6 | 29 | function App(): JSX.Element { |
7 | | - var [count, setCount] = useState<number>(0); |
8 | | - const clickHandler = useCallback(() => { |
9 | | - setCount((prev) => prev + 1); |
10 | | - }, [setCount]); |
| 30 | + const [isConditional, setIsConditional] = useState(true); |
| 31 | + const isConditionalRef = useRef(isConditional); |
| 32 | + const pivotGridRef = useRef<PivotGridRef>(null); |
| 33 | + |
| 34 | + isConditionalRef.current = isConditional; |
| 35 | + |
| 36 | + const calculateCustomSummary = useCallback((options: CustomSummaryValue) => { |
| 37 | + switch (options.summaryProcess) { |
| 38 | + case 'start': |
| 39 | + options.totalValue = { conditionalVal: 0, rawVal: 0, count: 0 }; |
| 40 | + break; |
| 41 | + case 'calculate': |
| 42 | + options.totalValue.count += 1; |
| 43 | + options.totalValue.rawVal += options.value.amount; |
| 44 | + if (options.value.isApproved) { |
| 45 | + options.totalValue.conditionalVal += options.value.amount; |
| 46 | + } |
| 47 | + break; |
| 48 | + case 'finalize': |
| 49 | + if (options.totalValue.count === 1 || !isConditionalRef.current) { |
| 50 | + options.totalValue = options.totalValue.rawVal; |
| 51 | + } else { |
| 52 | + options.totalValue = options.totalValue.conditionalVal; |
| 53 | + } |
| 54 | + break; |
| 55 | + default: break; |
| 56 | + } |
| 57 | + }, []); |
| 58 | + |
| 59 | + const dataSource = useMemo<DataSourceOptions>(() => ({ |
| 60 | + fields: [{ |
| 61 | + caption: 'Region', |
| 62 | + width: 120, |
| 63 | + dataField: 'region', |
| 64 | + area: 'row', |
| 65 | + expanded: true, |
| 66 | + }, { |
| 67 | + caption: 'City', |
| 68 | + dataField: 'city', |
| 69 | + width: 150, |
| 70 | + area: 'row', |
| 71 | + selector: citySelector, |
| 72 | + }, { |
| 73 | + dataField: 'date', |
| 74 | + dataType: 'date', |
| 75 | + area: 'column', |
| 76 | + expanded: true, |
| 77 | + }, { |
| 78 | + caption: 'Sales', |
| 79 | + dataType: 'number', |
| 80 | + summaryType: 'custom', |
| 81 | + format: 'currency', |
| 82 | + area: 'data', |
| 83 | + calculateCustomSummary, |
| 84 | + }, { |
| 85 | + caption: 'Approved', |
| 86 | + dataField: 'isApproved', |
| 87 | + summaryType: 'sum', |
| 88 | + area: 'data', |
| 89 | + }], |
| 90 | + store: sales, |
| 91 | + }), [calculateCustomSummary]); |
| 92 | + |
| 93 | + const onValueChanged = useCallback((arg: CheckBoxTypes.ValueChangedEvent) => { |
| 94 | + setIsConditional(arg.value); |
| 95 | + pivotGridRef.current?.instance().getDataSource().reload().catch(() => {}); |
| 96 | + }, []); |
| 97 | + |
11 | 98 | return ( |
12 | | - <div className="main"> |
13 | | - <Button text={`Click count: ${count}`} onClick={clickHandler} /> |
| 99 | + <div className="app"> |
| 100 | + <div className="long-title"><h3>Conditional Summary Calculation</h3></div> |
| 101 | + <PivotGrid id="sales" |
| 102 | + ref={pivotGridRef} |
| 103 | + allowSortingBySummary={true} |
| 104 | + allowSorting={true} |
| 105 | + allowFiltering={true} |
| 106 | + allowExpandAll={true} |
| 107 | + height={440} |
| 108 | + showBorders={true} |
| 109 | + dataSource={dataSource}> |
| 110 | + <FieldChooser enabled={false} /> |
| 111 | + </PivotGrid> |
| 112 | + |
| 113 | + <div className="options"> |
| 114 | + <div className="caption">Options</div> |
| 115 | + <div className="option"> |
| 116 | + <CheckBox |
| 117 | + value={isConditional} |
| 118 | + text="Toggle Conditional Summary Calculation" |
| 119 | + onValueChanged={onValueChanged} /> |
| 120 | + </div> |
| 121 | + </div> |
14 | 122 | </div> |
15 | 123 | ); |
16 | 124 | } |
|
0 commit comments