-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHomeContent.vue
More file actions
135 lines (127 loc) · 3.56 KB
/
HomeContent.vue
File metadata and controls
135 lines (127 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<template>
<div id="data-grid-demo">
<DxDataGrid
:data-source="data"
@init-new-row="onInitNewRow"
>
<DxEditing
:allow-updating="true"
:allow-adding="true"
mode="row"
/>
<DxColumn
:set-cell-value="setStateValue"
data-field="StateID"
caption="State"
:cell-template="arrayCellTemplate"
edit-cell-template="multipleDropDownBoxStateTemplate"
>
<DxLookup
:data-source="states"
display-expr="Name"
value-expr="ID"
/>
</DxColumn>
<DxColumn
data-field="CityID"
caption="City"
:cell-template="arrayCellTemplate"
edit-cell-template="multipleDropDownBoxCityTemplate"
>
<DxLookup
:data-source="getFilteredCities"
display-expr="Name"
value-expr="ID"
/>
</DxColumn>
<template #multipleDropDownBoxStateTemplate="{ data: cellInfo }">
<StateDropDownBoxComponent
:value="cellInfo.value"
:on-value-changed="cellInfo.setValue"
:data-source="states"
:cell-info="cellInfo"
/>
</template>
<template #multipleDropDownBoxCityTemplate="{ data: cellInfo }">
<CityDropDownBoxComponent
:value="cellInfo.value"
:on-value-changed="cellInfo.setValue"
:data-source="cityDataSource(cellInfo)"
:cell-info="cellInfo"
/>
</template>
</DxDataGrid>
</div>
</template>
<script setup lang="ts">
import 'devextreme/dist/css/dx.material.blue.light.compact.css';
import {
DxDataGrid,
DxColumn,
DxEditing,
DxLookup,
type DxDataGridTypes,
} from 'devextreme-vue/data-grid';
import service, { type City, type Employee } from '../data';
import StateDropDownBoxComponent from './StateDropDownBoxComponent.vue';
import CityDropDownBoxComponent from './CityDropDownBoxComponent.vue';
import { DataSource, ArrayStore } from 'devextreme-vue/common/data';
const data: Employee[] = service.getEmployees();
const states = service.getStates();
const cities = service.getCities();
const onInitNewRow = (e: DxDataGridTypes.InitNewRowEvent<Employee, number>): void => {
e.data.StateID = [];
e.data.CityID = [];
};
const setStateValue = async function(
this: DxDataGridTypes.Column<Employee, number>,
rowData: Employee,
value: number[],
currentRowData: Employee
) {
rowData.CityID = [];
await this.defaultSetCellValue?.(rowData, value, currentRowData);
};
const arrayCellTemplate = (
container: HTMLElement,
options: DxDataGridTypes.ColumnCellTemplateData<Employee, number>
): void => {
const noBreakSpace = '\u00A0';
const text = (options.value || [])
.map((element: number) => {
return options.column.lookup?.calculateCellValue?.(element);
})
.join(', ');
container.textContent = text || noBreakSpace;
container.title = text;
};
const getFilteredCities = (options: { data: Employee, key: number }): {
store: City[];
filter: [string, string, number[]] | null;
} => {
return {
store: cities,
filter: options.data ? ['StateID', '=', options.data.StateID] : null,
};
};
const cityDataSource = (
cellInfo: DxDataGridTypes.ColumnEditCellTemplateData<Employee, number>
): DataSource => {
return new DataSource({
store: new ArrayStore({
data: cities,
key: 'ID',
}),
filter: (data: City) => {
return (cellInfo.row.data?.StateID?.length > 0
? cellInfo.row.data.StateID.includes(data.StateID)
: true);
},
});
};
</script>
<style>
#data-grid-demo {
min-height: 700px;
}
</style>