|
1 | 1 | <script setup lang="ts"> |
2 | | -import { computed, ref } from 'vue'; |
3 | | -
|
4 | 2 | import 'devextreme/dist/css/dx.material.blue.light.compact.css'; |
5 | | -import DxButton from 'devextreme-vue/button'; |
| 3 | +import { |
| 4 | + DxDataGrid, |
| 5 | + DxColumn, |
| 6 | + DxEditing, |
| 7 | + DxLookup, |
| 8 | +} from 'devextreme-vue/data-grid'; |
| 9 | +import type { DxDataGridTypes } from 'devextreme-vue/data-grid'; |
| 10 | +import type { ValueChangedEvent } from 'devextreme/ui/lookup'; |
| 11 | +import type { Customer, Employee } from '../types'; |
| 12 | +import { customers, employees } from '../data'; |
| 13 | +
|
| 14 | +const onEditorPreparing = ( |
| 15 | + e: DxDataGridTypes.EditorPreparingEvent<Employee, number> |
| 16 | +): void => { |
| 17 | + if (e.parentType === 'dataRow' && e.dataField === 'CustomerID') { |
| 18 | + e.editorOptions.onValueChanged = function(ev: ValueChangedEvent): void { |
| 19 | + const selectedItem = ev.component.option('selectedItem') as Customer; |
| 20 | + if (!selectedItem || !e.setValue) return; |
| 21 | + e.setValue(selectedItem); |
| 22 | + }; |
| 23 | + } |
| 24 | +}; |
6 | 25 |
|
7 | | -const props = defineProps({ |
8 | | - text: { |
9 | | - type: String, |
10 | | - default: 'count', |
11 | | - }, |
12 | | -}); |
13 | | -const count = ref(0); |
14 | | -const buttonText = computed < string > ( |
15 | | - () => `Click ${props.text}: ${count.value}` |
16 | | -); |
17 | | -function clickHandler() { |
18 | | - count.value += 1; |
19 | | -} |
| 26 | +const setCellValue = (rowData: Employee, value: Customer): void => { |
| 27 | + if (!rowData || !value) return; |
| 28 | + rowData.CustomerID = value.CustomerID; |
| 29 | + rowData.Address = value.Address; |
| 30 | + rowData.Phone = value.Phone; |
| 31 | +}; |
20 | 32 | </script> |
| 33 | + |
21 | 34 | <template> |
22 | | - <div> |
23 | | - <DxButton |
24 | | - :text="buttonText" |
25 | | - @click="clickHandler" |
| 35 | + <DxDataGrid |
| 36 | + :data-source="employees" |
| 37 | + @editor-preparing="onEditorPreparing" |
| 38 | + > |
| 39 | + <DxEditing |
| 40 | + :allow-updating="true" |
| 41 | + :allow-adding="true" |
26 | 42 | /> |
27 | | - </div> |
| 43 | + <DxColumn |
| 44 | + caption="Name" |
| 45 | + data-field="CustomerID" |
| 46 | + :set-cell-value="setCellValue" |
| 47 | + > |
| 48 | + <DxLookup |
| 49 | + :data-source="customers" |
| 50 | + value-expr="CustomerID" |
| 51 | + display-expr="CustomerName" |
| 52 | + /> |
| 53 | + </DxColumn> |
| 54 | + <DxColumn data-field="Address"/> |
| 55 | + <DxColumn data-field="Phone"/> |
| 56 | + </DxDataGrid> |
28 | 57 | </template> |
0 commit comments