|
1 | | -<script setup lang="ts"> |
2 | | -import { computed, ref } from 'vue'; |
3 | | -
|
| 1 | +<script setup lang='ts'> |
| 2 | +import { ref } from 'vue'; |
4 | 3 | import 'devextreme/dist/css/dx.material.blue.light.compact.css'; |
5 | | -import DxButton from 'devextreme-vue/button'; |
| 4 | +import DxPivotGrid, { DxFieldChooser } from 'devextreme-vue/pivot-grid'; |
| 5 | +import DxCheckBox, { type DxCheckBoxTypes } from 'devextreme-vue/check-box'; |
| 6 | +import PivotGridDataSource from 'devextreme/ui/pivot_grid/data_source'; |
| 7 | +import service, { type Sale } from './data'; |
| 8 | +
|
| 9 | +const isConditional = ref(true); |
| 10 | +const pivotGridRef = ref<DxPivotGrid|null>(null); |
| 11 | +
|
| 12 | +function toggleConditionalChanged(e: DxCheckBoxTypes.ValueChangedEvent) { |
| 13 | + isConditional.value = e.value; |
| 14 | + pivotGridRef.value?.instance?.getDataSource().reload(); |
| 15 | +} |
| 16 | +
|
| 17 | +interface CustomSummaryValue { |
| 18 | + summaryProcess?: string; |
| 19 | + value?: any; |
| 20 | + totalValue?: any; |
| 21 | +} |
| 22 | +
|
| 23 | +const tempSales = service.getSales(); |
6 | 24 |
|
7 | | -const props = defineProps({ |
8 | | - text: { |
9 | | - type: String, |
10 | | - default: 'count', |
11 | | - }, |
| 25 | +tempSales.forEach((sale: Sale, index: number) => { |
| 26 | + sale.isApproved = index % 2 !== 0; |
12 | 27 | }); |
13 | | -const count = ref(0); |
14 | | -const buttonText = computed<string>( |
15 | | - () => `Click ${props.text}: ${count.value}` |
16 | | -); |
17 | | -function clickHandler() { |
18 | | - count.value += 1; |
| 28 | +
|
| 29 | +const sales = tempSales; |
| 30 | +
|
| 31 | +function calculateCustomSummary(options: CustomSummaryValue) { |
| 32 | + switch (options.summaryProcess) { |
| 33 | + case 'start': |
| 34 | + options.totalValue = { conditionalVal: 0, rawVal: 0, count: 0 }; |
| 35 | + break; |
| 36 | + case 'calculate': |
| 37 | + options.totalValue.count += 1; |
| 38 | + options.totalValue.rawVal += options.value.amount; |
| 39 | + if (options.value.isApproved) { |
| 40 | + options.totalValue.conditionalVal += options.value.amount; |
| 41 | + } |
| 42 | + break; |
| 43 | + case 'finalize': |
| 44 | + if (options.totalValue.count === 1 || !isConditional.value) { |
| 45 | + options.totalValue = options.totalValue.rawVal; |
| 46 | + } else { |
| 47 | + options.totalValue = options.totalValue.conditionalVal; |
| 48 | + } |
| 49 | + break; |
| 50 | + default: break; |
| 51 | + } |
19 | 52 | } |
| 53 | +
|
| 54 | +const dataSource: PivotGridDataSource = new PivotGridDataSource({ |
| 55 | + fields: [{ |
| 56 | + caption: 'Region', |
| 57 | + width: 120, |
| 58 | + dataField: 'region', |
| 59 | + area: 'row', |
| 60 | + expanded: true, |
| 61 | + }, { |
| 62 | + caption: 'City', |
| 63 | + dataField: 'city', |
| 64 | + width: 150, |
| 65 | + area: 'row', |
| 66 | + selector: (data: Sale): string => `${data.city} (${data.country})`, |
| 67 | + }, { |
| 68 | + dataField: 'date', |
| 69 | + dataType: 'date', |
| 70 | + area: 'column', |
| 71 | + expanded: true, |
| 72 | + }, { |
| 73 | + caption: 'Sales', |
| 74 | + dataType: 'number', |
| 75 | + summaryType: 'custom', |
| 76 | + format: 'currency', |
| 77 | + area: 'data', |
| 78 | + calculateCustomSummary, |
| 79 | + }, { |
| 80 | + caption: 'Approved', |
| 81 | + dataField: 'isApproved', |
| 82 | + summaryType: 'sum', |
| 83 | + area: 'data', |
| 84 | + }], |
| 85 | + store: sales, |
| 86 | +}); |
| 87 | +
|
20 | 88 | </script> |
21 | 89 | <template> |
22 | 90 | <div> |
23 | | - <DxButton |
24 | | - :text="buttonText" |
25 | | - @click="clickHandler" |
26 | | - /> |
| 91 | + <div class='long-title'> |
| 92 | + <h3>Conditional Summary Calculation</h3> |
| 93 | + </div> |
| 94 | + <DxPivotGrid |
| 95 | + ref='pivotGridRef' |
| 96 | + :allow-sorting-by-summary='true' |
| 97 | + :allow-sorting='true' |
| 98 | + :allow-filtering='true' |
| 99 | + :allow-expand-all='true' |
| 100 | + :height='440' |
| 101 | + :show-borders='true' |
| 102 | + :data-source='dataSource' |
| 103 | + > |
| 104 | + <DxFieldChooser :enabled='false'/> |
| 105 | + </DxPivotGrid> |
| 106 | + <div class='options'> |
| 107 | + <div class='caption'>Options</div> |
| 108 | + <div class='option'> |
| 109 | + <DxCheckBox |
| 110 | + :value.sync='isConditional' |
| 111 | + text='Toggle Conditional Summary Calculation' |
| 112 | + @value-changed='toggleConditionalChanged' |
| 113 | + /> |
| 114 | + </div> |
| 115 | + </div> |
27 | 116 | </div> |
28 | 117 | </template> |
| 118 | +<style scoped> |
| 119 | +#sales { |
| 120 | + margin-top: 80px; |
| 121 | +} |
| 122 | +
|
| 123 | +.long-title h3 { |
| 124 | + font-family: 'Segoe UI Light', 'Helvetica Neue Light', 'Segoe UI', |
| 125 | + 'Helvetica Neue', 'Trebuchet MS', Verdana; |
| 126 | + font-weight: 200; |
| 127 | + font-size: 28px; |
| 128 | + text-align: center; |
| 129 | + margin-bottom: 20px; |
| 130 | +} |
| 131 | +
|
| 132 | +.options { |
| 133 | + margin-top: 20px; |
| 134 | + padding: 20px; |
| 135 | + background: #f5f5f5; |
| 136 | +} |
| 137 | +
|
| 138 | +.options .caption { |
| 139 | + font-size: 18px; |
| 140 | + font-weight: 500; |
| 141 | +} |
| 142 | +
|
| 143 | +.option { |
| 144 | + margin-top: 10px; |
| 145 | +} |
| 146 | +
|
| 147 | +.option > span { |
| 148 | + width: 120px; |
| 149 | + display: inline-block; |
| 150 | +} |
| 151 | +
|
| 152 | +.option > .dx-widget { |
| 153 | + display: inline-block; |
| 154 | + vertical-align: middle; |
| 155 | + width: 100%; |
| 156 | + max-width: 350px; |
| 157 | +} |
| 158 | +</style> |
0 commit comments