How can I help you?
Data binding in React List box component
21 Feb 202613 minutes to read
The ListBox loads data from local or remote sources using the dataSource property. It supports both array and DataManager data types.
To get started quickly with Data Binding, watch this video:
| Fields | Type | Description |
|---|---|---|
text |
string |
Specifies the display text of each list item. |
value |
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 iconCss class that needs to be mapped. |
htmlAttributes |
string |
Allows additional attributes to configure the elements in various ways to meet the criteria. |
When binding complex data to the ListBox, fields should be mapped correctly. Otherwise, the selected item remains undefined.
Local Data
Local data can be represented by the following ways as described below.
Array of string
The ListBox supports loading primitive data arrays such as strings or numbers. For primitive data, both the value and text fields represent the same data.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
function App() {
let data = ['Badminton', 'Cricket', 'Football', 'Golf', 'Tennis', 'Basket Ball', 'Base Ball', 'Hockey', 'Volley Ball'];
return (<ListBoxComponent dataSource={data}/>);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
function App() {
let data: string[] = ['Badminton', 'Cricket', 'Football', 'Golf', 'Tennis', 'Basket Ball', 'Base Ball', 'Hockey', 'Volley Ball'];
return (
<ListBoxComponent dataSource={data} />
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));Array of object
The ListBox generates list items from an array of object data. Map the appropriate columns to the fields property.
In the following example, the id and sports columns are mapped to the value and text fields respectively.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
function App() {
// define the array of object
let data = [
{ text: 'Hennessey Venom', id: 'list-01' },
{ text: 'Bugatti Chiron', id: 'list-02' },
{ text: 'Bugatti Veyron Super Sport', id: 'list-03' },
{ text: 'SSC Ultimate Aero', id: 'list-04' },
{ text: 'Koenigsegg CCR', id: 'list-05' },
{ text: 'McLaren F1', id: 'list-06' },
{ text: 'Aston Martin One- 77', id: 'list-07' },
{ text: 'Jaguar XJ220', id: 'list-08' },
{ text: 'McLaren P1', id: 'list-09' },
{ text: 'Ferrari LaFerrari', id: 'list-10' },
];
return (<ListBoxComponent dataSource={data}/>);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
function App() {
// define the array of object
let data: { [key: string]: Object }[] = [
{ text: 'Hennessey Venom', id: 'list-01' },
{ text: 'Bugatti Chiron', id: 'list-02' },
{ text: 'Bugatti Veyron Super Sport', id: 'list-03' },
{ text: 'SSC Ultimate Aero', id: 'list-04' },
{ text: 'Koenigsegg CCR', id: 'list-05' },
{ text: 'McLaren F1', id: 'list-06' },
{ text: 'Aston Martin One- 77', id: 'list-07' },
{ text: 'Jaguar XJ220', id: 'list-08' },
{ text: 'McLaren P1', id: 'list-09' },
{ text: 'Ferrari LaFerrari', id: 'list-10' },
];
return (
<ListBoxComponent dataSource={data} />
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));Array of complex object
The ListBox generates list items from complex nested data. Map the appropriate nested columns to the fields property.
In the following example, the sports.Name column is mapped to the text field.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
function App() {
// define the array of object
let data = [
{ id: 'game1', sports: { Name: 'Badminton' } },
{ id: 'game2', sports: { Name: 'Cricket' } },
{ id: 'game3', sports: { Name: 'Football' } },
{ id: 'game4', sports: { Name: 'Golf' } },
{ id: 'game5', sports: { Name: 'Tennis' } },
{ id: 'game6', sports: { Name: 'Basket Ball' } },
{ id: 'game7', sports: { Name: 'Base Ball' } },
{ id: 'game8', sports: { Name: 'Hockey' } },
{ id: 'game9', sports: { Name: 'Volley Ball' } }
];
let fields = { text: "sports.Name", value: "id" };
return (<ListBoxComponent dataSource={data} fields={fields}/>);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
function App() {
// define the array of object
let data: { [key: string]: Object }[] = [
{ id: 'game1', sports: { Name: 'Badminton'} },
{ id: 'game2', sports: { Name: 'Cricket'} },
{ id: 'game3', sports: { Name: 'Football'} },
{ id: 'game4', sports: { Name: 'Golf'} },
{ id: 'game5', sports: { Name: 'Tennis'} },
{ id: 'game6', sports: { Name: 'Basket Ball'} },
{ id: 'game7', sports: { Name: 'Base Ball'} },
{ id: 'game8', sports: { Name: 'Hockey'} },
{ id: 'game9', sports: { Name: 'Volley Ball'} }
];
let fields: object = { text:"sports.Name", value:"id"};
return (
<ListBoxComponent dataSource={data} fields={fields} />
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));Remote Data
The ListBox supports retrieval of data from remote data services with the help of DataManager component.
The following sample displays the employee names from Employee table.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
import { DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
function App() {
let fields = { text: 'FirstName', value: 'EmployeeID' };
let data = new DataManager({
adaptor: new ODataV4Adaptor,
crossDomain: true,
url: 'https://ej2services.syncfusion.com/production/web-services/api/Employees'
});
return (
// specifies the tag for render the ListBox component
<ListBoxComponent dataSource={data} fields={fields}/>);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
import { DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
function App() {
let fields: object = { text:'FirstName', value:'EmployeeID'};
let data:DataManager = new DataManager({
adaptor: new ODataV4Adaptor,
crossDomain: true,
url: 'https://ej2services.syncfusion.com/production/web-services/api/Employees'
});
return (
// specifies the tag for render the ListBox component
<ListBoxComponent dataSource={data} fields={fields} />
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));