Data binding in React List box component

13 Dec 202313 minutes to read

The ListBox loads the data either from local data sources or remote data services using the dataSource property. It supports
the data type of array or DataManager.

To get start quickly with Data Binding, you can check on 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 has support to load array of primitive data such as strings or numbers. Here, both value and text field acts as same.

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 can generate its list items through an array of object data. For this, the appropriate columns should be mapped to the fields property.

In the following example, id and sports column from complex data have been mapped to the value field and text field, 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 can generate its list items through an array of complex data. For this, the appropriate columns should be mapped to the fields property.

In the following example, sports.Name column from complex data have been 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'));