|
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 DataGrid, { |
| 3 | + Editing, Column, Lookup, type DataGridTypes, |
| 4 | +} from 'devextreme-react/data-grid'; |
| 5 | +import './App.css'; |
| 6 | +import ArrayStore from 'devextreme/data/array_store'; |
| 7 | +import { DataSource } from 'devextreme-react/common/data'; |
| 8 | +import service, { type City, type Employee } from './data'; |
| 9 | +import MultipleDropDownBox from './components/MultipleDropDownBox'; |
| 10 | + |
| 11 | +const dataSource: Employee[] = service.getEmployees(); |
| 12 | + |
| 13 | +const statesStore = new ArrayStore({ |
| 14 | + data: service.getStates(), |
| 15 | + key: 'ID', |
| 16 | +}); |
| 17 | + |
| 18 | +const citiesStore = new ArrayStore({ |
| 19 | + data: service.getCities(), |
| 20 | + key: 'ID', |
| 21 | +}); |
| 22 | + |
| 23 | +function getFilteredCities(cellInfo: DataGridTypes.ColumnEditCellTemplateData<Employee, number>, citiesStore: ArrayStore): DataSource { |
| 24 | + return new DataSource({ |
| 25 | + store: citiesStore, |
| 26 | + filter: (data: City) => (cellInfo.data?.StateID && cellInfo.data?.StateID?.length > 0 |
| 27 | + ? cellInfo.data?.StateID.includes(data.StateID) |
| 28 | + : true), |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +async function setStateValue(this: DataGridTypes.Column<Employee, number>, rowData: Employee, value: number[], currentRowData: Employee): Promise<void> { |
| 33 | + rowData.CityID = []; |
| 34 | + await this.defaultSetCellValue?.(rowData, value, currentRowData); |
| 35 | +} |
| 36 | + |
| 37 | +function arrayCellTemplate(container: HTMLElement, options: DataGridTypes.ColumnCellTemplateData<Employee, number>): void { |
| 38 | + const noBreakSpace = '\u00A0'; |
| 39 | + const text = (options.value || []) |
| 40 | + .map((element: number) => options.column.lookup?.calculateCellValue?.(element) as string || '') |
| 41 | + .join(', '); |
| 42 | + container.textContent = text || noBreakSpace; |
| 43 | + container.title = text; |
| 44 | +} |
| 45 | + |
| 46 | +function renderMultipleDropDownBox( |
| 47 | + currentValue: number[], |
| 48 | + // eslint-disable-next-line no-unused-vars |
| 49 | + setValue: (value: number[]) => void, |
| 50 | + dataSource: ArrayStore | DataSource, |
| 51 | +): JSX.Element { |
| 52 | + return ( |
| 53 | + <MultipleDropDownBox |
| 54 | + dataSource={dataSource} |
| 55 | + value={currentValue} |
| 56 | + setValue={setValue} |
| 57 | + /> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +function renderStateDropDownBox(e: DataGridTypes.ColumnEditCellTemplateData<Employee, number>): JSX.Element { |
| 62 | + const { setValue } = e; |
| 63 | + return renderMultipleDropDownBox( |
| 64 | + e.value, |
| 65 | + setValue, |
| 66 | + statesStore, |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +function renderCityDropDownBox(e: DataGridTypes.ColumnEditCellTemplateData<Employee, number>): JSX.Element { |
| 71 | + const { setValue } = e; |
| 72 | + return renderMultipleDropDownBox( |
| 73 | + e.value, |
| 74 | + setValue, |
| 75 | + getFilteredCities(e, citiesStore), |
| 76 | + ); |
| 77 | +} |
5 | 78 |
|
6 | 79 | function App(): JSX.Element { |
7 | | - var [count, setCount] = useState<number>(0); |
8 | | - const clickHandler = useCallback(() => { |
9 | | - setCount((prev) => prev + 1); |
10 | | - }, [setCount]); |
11 | 80 | return ( |
12 | | - <div className="main"> |
13 | | - <Button text={`Click count: ${count}`} onClick={clickHandler} /> |
| 81 | + <div className="App"> |
| 82 | + <DataGrid dataSource={dataSource}> |
| 83 | + <Editing |
| 84 | + mode="row" |
| 85 | + allowUpdating={true} |
| 86 | + allowAdding={true}> |
| 87 | + </Editing> |
| 88 | + <Column |
| 89 | + dataField="StateID" |
| 90 | + caption="State" |
| 91 | + setCellValue={setStateValue} |
| 92 | + cellTemplate={arrayCellTemplate} |
| 93 | + editCellRender={renderStateDropDownBox}> |
| 94 | + <Lookup dataSource={statesStore} displayExpr="Name" valueExpr="ID" /> |
| 95 | + </Column> |
| 96 | + <Column |
| 97 | + dataField="CityID" |
| 98 | + caption="City" |
| 99 | + cellTemplate={arrayCellTemplate} |
| 100 | + editCellRender={renderCityDropDownBox}> |
| 101 | + <Lookup dataSource={citiesStore} displayExpr="Name" valueExpr="ID" /> |
| 102 | + </Column> |
| 103 | + </DataGrid> |
14 | 104 | </div> |
15 | 105 | ); |
16 | 106 | } |
|
0 commit comments