Data Binding in React MultiSelect Dropdown
4 Sep 202624 minutes to read
The React MultiSelect loads data from local sources or remote services using the dataSource property. The dataSource accepts either a local array of values or a DataManager instance that handles remote data services.
The DataManager provides configuration options such as the url, adaptor, and crossDomain properties to connect to remote services. It supports various data services including OData, OData V4, and Web API, with support for XML, JSON, and JSONP formats through DataManager adaptors. For the full list of available adaptors and configuration options, see the DataManager documentation.
| Fields | Type | Description |
|---|---|---|
| text | string |
Specifies the display text of each list item. |
| value |
number or string
|
Specifies the hidden data value mapped to each list item that should contain a unique value. |
| groupBy | string |
Specifies the category under which the list item has to be grouped. |
| iconCss | string |
Specifies the icon class of each list item. |
When binding complex data to the React MultiSelect, fields should be mapped correctly. Otherwise, the selected item remains undefined.
Binding local data
Local data can be represented in three ways as described below.
1. Array of strings
The React MultiSelect supports loading arrays of primitive data such as strings and numbers. In this case, the value and text fields represent the same data.
[Class-component]
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
// define the array of data
sportsData = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby', 'Snooker', 'Tennis'];
render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={this.sportsData} popupHeight="250px" popupWidth="250px" placeholder="Find a game"/>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component<{}, {}> {
// define the array of data
private sportsData: string[] = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby','Snooker', 'Tennis'];
public render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={this.sportsData} popupHeight="250px" popupWidth="250px" placeholder="Find a game" />
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));[Functional-component]
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the array of data
let sportsData = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby', 'Snooker', 'Tennis'];
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={sportsData} popupHeight="250px" popupWidth="250px" placeholder="Find a game"/>);
}
ReactDOM.render(<App />, document.getElementById('sample'));import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the array of data
let sportsData: string[] = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby','Snooker', 'Tennis'];
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={sportsData} popupHeight="250px" popupWidth="250px" placeholder="Find a game" />
);
}
ReactDOM.render(<App />, document.getElementById('sample'));2. Array of objects
The React MultiSelect generates list items from arrays of objects. Map the appropriate columns to the fields property.
In the following example, the id column is mapped to the value field and the sports column is mapped to the text field.
[Class-component]
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
// define the JSON of data
sportsData = [
{ id: 'game1', sports: 'Badminton' },
{ id: 'game2', sports: 'Football' },
{ id: 'game3', sports: 'Tennis' }
];
// maps the appropriate column to fields property
fields = { text: 'sports', value: 'id' };
render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={this.sportsData} fields={this.fields} placeholder="Select a game"/>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component<{}, {}> {
// define the JSON of data
private sportsData: { [key: string]: Object }[] = [
{ id: 'game1', sports: 'Badminton' },
{ id: 'game2', sports: 'Football' },
{ id: 'game3', sports: 'Tennis' }
];
// maps the appropriate column to fields property
private fields: object = { text: 'sports', value: 'id' };
public render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={this.sportsData} fields={this.fields} placeholder="Select a game" />
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));[Functional-component]
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the JSON of data
const sportsData = [
{ id: 'game1', sports: 'Badminton' },
{ id: 'game2', sports: 'Football' },
{ id: 'game3', sports: 'Tennis' }
];
// maps the appropriate column to fields property
const fields = { text: 'sports', value: 'id' };
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={sportsData} fields={fields} placeholder="Select a game"/>);
}
ReactDOM.render(<App />, document.getElementById('sample'));import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the JSON of data
const sportsData: { [key: string]: Object }[] = [
{ id: 'game1', sports: 'Badminton' },
{ id: 'game2', sports: 'Football' },
{ id: 'game3', sports: 'Tennis' }
];
// maps the appropriate column to fields property
const fields: object = { text: 'sports', value: 'id' };
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={sportsData} fields={fields} placeholder="Select a game" />
);
}
ReactDOM.render(<App />, document.getElementById('sample'));3. Array of complex objects
The React MultiSelect generates list items from arrays of complex objects by mapping nested properties to the fields property.
In the following example, Code.Id is mapped to the value field and Country.Name is mapped to the text field.
[Class-component]
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
// define the JSON of data
countriesData = [
{ Country: { Name: 'Australia' }, Code: { Id: 'AU' } },
{ Country: { Name: 'Bermuda' }, Code: { Id: 'BM' } },
{ Country: { Name: 'Canada' }, Code: { Id: 'CA' } },
{ Country: { Name: 'Cameroon' }, Code: { Id: 'CM' } },
{ Country: { Name: 'Denmark' }, Code: { Id: 'DK' } },
{ Country: { Name: 'France' }, Code: { Id: 'FR' } }
];
// maps the appropriate column to fields property
fields = { text: 'Country.Name', value: 'Code.Id' };
render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={this.countriesData} fields={this.fields} placeholder="Select a country"/>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component<{}, {}> {
// define the JSON of data
private countriesData: { [key: string]: Object }[] = [
{ Country: { Name: 'Australia' }, Code: { Id: 'AU' }},
{ Country: { Name: 'Bermuda' },Code: { Id: 'BM' }},
{ Country:{ Name: 'Canada'}, Code:{ Id: 'CA'} },
{ Country:{Name: 'Cameroon'}, Code:{ Id: 'CM'} },
{ Country:{Name: 'Denmark'}, Code:{ Id: 'DK' }},
{ Country:{Name: 'France'}, Code: { Id:'FR'} }
];
// maps the appropriate column to fields property
private fields: object = { text: 'Country.Name', value: 'Code.Id' };
public render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={this.countriesData} fields={this.fields} placeholder="Select a country" />
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));[Functional-component]
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the JSON of data
const countriesData = [
{ Country: { Name: 'Australia' }, Code: { Id: 'AU' } },
{ Country: { Name: 'Bermuda' }, Code: { Id: 'BM' } },
{ Country: { Name: 'Canada' }, Code: { Id: 'CA' } },
{ Country: { Name: 'Cameroon' }, Code: { Id: 'CM' } },
{ Country: { Name: 'Denmark' }, Code: { Id: 'DK' } },
{ Country: { Name: 'France' }, Code: { Id: 'FR' } }
];
// maps the appropriate column to fields property
const fields = { text: 'Country.Name', value: 'Code.Id' };
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={countriesData} fields={fields} placeholder="Select a country"/>);
}
ReactDOM.render(<App />, document.getElementById('sample'));import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the JSON of data
const countriesData: { [key: string]: Object }[] = [
{ Country: { Name: 'Australia' }, Code: { Id: 'AU' }},
{ Country: { Name: 'Bermuda' },Code: { Id: 'BM' }},
{ Country:{ Name: 'Canada'}, Code:{ Id: 'CA'} },
{ Country:{Name: 'Cameroon'}, Code:{ Id: 'CM'} },
{ Country:{Name: 'Denmark'}, Code:{ Id: 'DK' }},
{ Country:{Name: 'France'}, Code: { Id:'FR'} }
];
// maps the appropriate column to fields property
const fields: object = { text: 'Country.Name', value: 'Code.Id' };
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" dataSource={countriesData} fields={fields} placeholder="Select a country" />
);
}
ReactDOM.render(<App />, document.getElementById('sample'));Binding remote data
The React MultiSelect supports retrieval of data from remote data services using the DataManager component. The Query property is used to fetch data from the database and bind it to the React MultiSelect.
To bind remote data, import the DataManager and Query classes from the @syncfusion/ej2-data package, then bind a DataManager instance to the dataSource property and a Query instance to the query property of the React MultiSelect.
import { DataManager, Query } from '@syncfusion/ej2-data';
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';The following sample displays the first 6 contacts from the Customers table of the Northwind OData service, using a Query with .take(6).
[Class-component]
// import DataManager related classes
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
// bind the DataManager instance to dataSource property
customerData = new DataManager({
adaptor: new ODataV4Adaptor,
crossDomain: true,
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/',
});
// bind the Query instance to query property
query = new Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
// maps the appropriate column to fields property
fields = { text: 'ContactName', value: 'CustomerID' };
// sort the resulted items
sortOrder = 'Ascending';
render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" query={this.query} dataSource={this.customerData} fields={this.fields} sortOrder={this.sortOrder} placeholder="Select a customer"/>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));// import DataManager related classes
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { SortOrder } from '@syncfusion/ej2-lists';
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component<{}, {}> {
// bind the DataManager instance to dataSource property
private customerData: DataManager = new DataManager({
adaptor: new ODataV4Adaptor,
crossDomain: true,
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/',
});
// bind the Query instance to query property
private query: Query = new Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
// maps the appropriate column to fields property
private fields: object = { text: 'ContactName', value: 'CustomerID' };
// sort the resulted items
private sortOrder: SortOrder = 'Ascending';
public render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" query={this.query} dataSource={this.customerData}
fields={this.fields} sortOrder={this.sortOrder} placeholder="Select a customer" />
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));[Functional-component]
// import DataManager related classes
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// bind the DataManager instance to dataSource property
const customerData = new DataManager({
adaptor: new ODataV4Adaptor,
crossDomain: true,
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/',
});
// bind the Query instance to query property
const query = new Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
// maps the appropriate column to fields property
const fields = { text: 'ContactName', value: 'CustomerID' };
// sort the resulted items
const sortOrder = 'Ascending';
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" query={query} dataSource={customerData} fields={fields} sortOrder={sortOrder} placeholder="Select a customer"/>);
}
ReactDOM.render(<App />, document.getElementById('sample'));// import DataManager related classes
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { SortOrder } from '@syncfusion/ej2-lists';
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App(){
// bind the DataManager instance to dataSource property
const customerData: DataManager = new DataManager({
adaptor: new ODataV4Adaptor,
crossDomain: true,
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/',
});
// bind the Query instance to query property
const query: Query = new Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
// maps the appropriate column to fields property
const fields: object = { text: 'ContactName', value: 'CustomerID' };
// sort the resulted items
const sortOrder: SortOrder = 'Ascending';
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" query={query} dataSource={customerData}
fields={fields} sortOrder={sortOrder} placeholder="Select a customer" />
);
}
ReactDOM.render(<App />, document.getElementById('sample'));