JavaScript/jQuery 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.

  1. 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" },
    • // ...
    • ];
  2. 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:

  3. 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:

    1. 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.
    2. Update the selection
      Implement the DropDownBox's onValueChanged handler to update the selection when the DropDownBox's value changes.
    3. Update the DropDownBox's value
      Implement the embedded UI component's onSelectionChanged handler to update the DropDownBox's value when the selection changes.
    IMPORTANT
    Ensure the type of value matches the selectedRowKeys/selectedItemKeys type (such as Array).

Single Selection Demo Multiple Selection Demo

View on GitHub View on GitHub