Value Binding in React ComboBox
24 Sep 20268 minutes to read
Value binding in the React ComboBox control allows you to associate data values with each list item. This facilitates managing and retrieving selected values efficiently. The React ComboBox component provides flexibility in binding both primitive data types and complex objects.
Primitive Data Types
The React ComboBox provides flexible binding for primitive data types such as strings and numbers. Bind local arrays, remote data sources, or customize binding to meet specific requirements. Set the value of primitive data using the value property.
Supported primitive data types:
- String
- Number
- Boolean
- Null
The following example demonstrates preselecting values with primitive data types.
[Class-component]
import { ComboBoxComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
constructor(props) {
super(props);
this.records = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5" , "Item 6", "Item 7", "Item 8", "Item 9", "Item 10"]
}
fields = { text: 'text', value: 'id' };
value = "Item 5";
render() {
return (
// specifies the tag for render the ComboBox component
<ComboBoxComponent id="datas" dataSource={this.records} value={this.value} placeholder="e.g. Item 1" allowFiltering={false} popupHeight="200px" >
</ComboBoxComponent>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));import { ComboBoxComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component<{}, {}> {
// maps the appropriate column to fields property
private fields: object = { text: 'text', value: 'id' };
// define the array of string
private records: string[] = [];
private value : string = "Item 1";
constructor(props) {
super(props);
this.records = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10"];
}
public render() {
return (
// specifies the tag for render the ComboBox component
<ComboBoxComponent id="datas" dataSource={this.records} value={this.value} placeholder="e.g. Item 1" allowObjectBinding={true} allowFiltering={false} popupHeight="200px" >
</ComboBoxComponent>
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));Object Data Types
Object binding in the React ComboBox allows you to bind a dataset of objects. When allowObjectBinding is enabled, the component’s value becomes an object of the same type as the selected item in the value property. This seamlessly binds object arrays from local sources, remote endpoints, or custom implementations.
The following example demonstrates preselecting values with object data types.
[Class-component]
import { ComboBoxComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
constructor(props) {
super(props);
this.records = Array.from({ length: 150 }, (_, i) => ({
id: 'id' + (i + 1),
text: `Item ${i + 1}`,
}));
}
fields = { text: 'text', value: 'id' };
value = { text: 'Item 5', value: 'id5'};
render() {
return (
// specifies the tag for render the ComboBox component
<ComboBoxComponent id="datas" dataSource={this.records} value={this.value} fields={this.fields} placeholder="e.g. Item 1" allowObjectBinding={true} allowFiltering={false} popupHeight="200px" >
</ComboBoxComponent>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));import { ComboBoxComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component<{}, {}> {
// maps the appropriate column to fields property
private fields: object = { text: 'text', value: 'id' };
// define the array of string
private records: { [key: string]: Object }[] = [];
private value : { [key: string]: Object } = { id: 'id1', text: 'Item 1' };
constructor(props) {
super(props);
this.records = Array.from({ length: 150 }, (_, i) => ({
id: 'id' + (i + 1),
text: `Item ${i + 1}`,
}));
}
public render() {
return (
// specifies the tag for render the ComboBox component
<ComboBoxComponent id="datas" dataSource={this.records} value={this.value} fields={this.fields} placeholder="e.g. Item 1" allowObjectBinding={true} allowFiltering={false} popupHeight="200px" >
</ComboBoxComponent>
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));