-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApp.tsx
More file actions
107 lines (96 loc) · 3.21 KB
/
App.tsx
File metadata and controls
107 lines (96 loc) · 3.21 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
import 'devextreme/dist/css/dx.material.blue.light.compact.css';
import DataGrid, {
Editing, Column, Lookup, type DataGridTypes,
} from 'devextreme-react/data-grid';
import './App.css';
import ArrayStore from 'devextreme/data/array_store';
import { type DataSourceOptions } from 'devextreme-react/common/data';
import service, { type City, type Employee } from './data';
import MultipleDropDownBox from './components/MultipleDropDownBox';
const dataSource: Employee[] = service.getEmployees();
const statesStore = new ArrayStore({
data: service.getStates(),
key: 'ID',
});
const citiesStore = new ArrayStore({
data: service.getCities(),
key: 'ID',
});
function getFilteredCities(cellInfo: DataGridTypes.ColumnEditCellTemplateData<Employee, number>, citiesArrayStore: ArrayStore): DataSourceOptions {
return {
store: citiesArrayStore,
filter: (data: City) => (cellInfo.data?.StateID && cellInfo.data?.StateID?.length > 0
? cellInfo.data?.StateID.includes(data.StateID)
: true),
};
}
async function setStateValue(this: DataGridTypes.Column<Employee, number>, rowData: Employee, value: number[], currentRowData: Employee): Promise<void> {
rowData.CityID = [];
await this.defaultSetCellValue?.(rowData, value, currentRowData);
}
function arrayCellTemplate(container: HTMLElement, options: DataGridTypes.ColumnCellTemplateData<Employee, number>): void {
const noBreakSpace = '\u00A0';
const text = (options.value || [])
.map((element: number) => options.column.lookup?.calculateCellValue?.(element) as string || '')
.join(', ');
container.textContent = text || noBreakSpace;
container.title = text;
}
function renderMultipleDropDownBox(
currentValue: number[],
setValue: (value: number[]) => void,
dropDownDataSource: ArrayStore | DataSourceOptions,
): JSX.Element {
return (
<MultipleDropDownBox
dataSource={dropDownDataSource}
value={currentValue}
setValue={setValue}
/>
);
}
function renderStateDropDownBox(e: DataGridTypes.ColumnEditCellTemplateData<Employee, number>): JSX.Element {
const { setValue } = e;
return renderMultipleDropDownBox(
e.value,
setValue,
statesStore,
);
}
function renderCityDropDownBox(e: DataGridTypes.ColumnEditCellTemplateData<Employee, number>): JSX.Element {
const { setValue } = e;
return renderMultipleDropDownBox(
e.value,
setValue,
getFilteredCities(e, citiesStore),
);
}
function App(): JSX.Element {
return (
<div className="App">
<DataGrid dataSource={dataSource}>
<Editing
mode="row"
allowUpdating={true}
allowAdding={true}>
</Editing>
<Column
dataField="StateID"
caption="State"
setCellValue={setStateValue}
cellTemplate={arrayCellTemplate}
editCellRender={renderStateDropDownBox}>
<Lookup dataSource={statesStore} displayExpr="Name" valueExpr="ID" />
</Column>
<Column
dataField="CityID"
caption="City"
cellTemplate={arrayCellTemplate}
editCellRender={renderCityDropDownBox}>
<Lookup dataSource={citiesStore} displayExpr="Name" valueExpr="ID" />
</Column>
</DataGrid>
</div>
);
}
export default App;