- Primitive Data Types
- Object Data Types
Contact Support
Value binding in ComboBox Component
14 Mar 20248 minutes to read
Value binding in the ComboBox control allows you to associate data values with each list item. This facilitates managing and retrieving selected values efficiently. The ComboBox component provides flexibility in binding both primitive data types and complex objects.
Primitive Data Types
The ComboBox control provides flexible binding capabilities for primitive data types like strings and numbers. You can effortlessly bind local primitive data arrays, fetch and bind data from remote sources, and even custom data binding to suit specific requirements. Bind the value of primitive data to the value property of the ComboBox.
Primitive data types include:
- String
- Number
- Boolean
- Null
The following sample shows the example for preselect values for primitive data type
[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
In the ComboBox control, object binding allows you to bind to a dataset of objects. When allowObjectBinding
is enabled, the value of the control will be an object of the same type as the selected item in the value property. This feature seamlessly binds arrays of objects, whether sourced locally, retrieved from remote endpoints, or customized to suit specific application needs.
The following sample shows the example for preselect values for object data type
[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'));