React DropDownBox - Synchronize with the Embedded Element
To use DropDownBox with an embedded UI component, synchronize the two components' data and selection state. The steps below use an embedded DataGrid as an example. The same approach applies to other DevExtreme UI components.
Specify data sources
The DropDownBox and embedded UI component can have the same data source or different data sources. If the data sources are different, the UI component's key field must be present in the DropDownBox data source.JavaScript- // Different data sources, both have the ID field
- const widgetData = [
- { ID: 1, fullName: "John Heart", position: "CEO"},
- { ID: 2, fullName: "Samantha Bright", position: "COO", headID: 1 },
- // ...
- ];
- const dropDownBoxData = [
- { ID: 1, email: "jheart@dx-email.com" },
- { ID: 2, email: "samanthab@dx-email.com" },
- // ...
- ];
Specify the shared key field
Assign the field's name to the DropDownBox's valueExpr property and to the key property of the embedded UI component's store. The following example uses an ArrayStore:App.tsx- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import { DropDownBox } from 'devextreme-react/drop-down-box';
- import { DataGrid } from 'devextreme-react/data-grid';
- import ArrayStore from 'devextreme/data/array_store';
- const dropDownBoxData = [/* ... */];
- const gridDataSource = new ArrayStore({
- data: widgetData,
- key: 'ID',
- });
- export default function App() {
- return (
- <DropDownBox
- dataSource={dropDownBoxData}
- valueExpr="ID"
- displayExpr="email">
- <DataGrid dataSource={gridDataSource} />
- </DropDownBox>
- );
- }
Synchronize the DropDownBox's value and the embedded UI component's selection
The synchronization implementation depends on the embedded UI component's API and the library/framework you use. If the library/framework supports two-way binding, you can bind the DropDownBox's value and the UI component's selectedRowKeys/selectedItemKeys to the same variable. If not, handle events as follows:- Set the initial selection in the embedded UI component
Implement the UI component's onContentReady handler to select data items according to the DropDownBox's initial value. In some UI components, you can set selectedRowKeys or selectedItemKeys directly instead of using onContentReady. - Update the selection
Implement the DropDownBox's onValueChanged handler to update the selection when the DropDownBox's value changes. - Update the DropDownBox's value
Implement the embedded UI component's onSelectionChanged handler to update the DropDownBox's value when the selection changes.
IMPORTANTEnsure the type of value matches the selectedRowKeys/selectedItemKeys type (such as Array).App.tsx- import React, { useState, useRef } from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import { DropDownBox, DropDownBoxRef } from 'devextreme-react/drop-down-box';
- import { DataGrid, Selection } from 'devextreme-react/data-grid';
- import type { SelectionChangedEvent } from 'devextreme/ui/data_grid';
- export default function App() {
- const [dropDownBoxValues, setDropDownBoxValues] = useState<number[]>([1]);
- const dropDownBoxRef = useRef<DropDownBoxRef>(null);
- const changeDropDownBoxValue = (e: SelectionChangedEvent) => {
- setDropDownBoxValues(e.selectedRowKeys as number[]);
- dropDownBoxRef.current?.instance().close();
- };
- return (
- <DropDownBox
- ref={dropDownBoxRef}
- value={dropDownBoxValues}>
- <DataGrid
- selectedRowKeys={dropDownBoxValues}
- onSelectionChanged={changeDropDownBoxValue}>
- <Selection mode="multiple" />
- </DataGrid>
- </DropDownBox>
- );
- }
- Set the initial selection in the embedded UI component