Value Binding in React AutoComplete
24 Sep 20268 minutes to read
Value binding in the React AutoComplete component allows you to associate data values with each list item. This makes it easier to manage and retrieve the selected value. The React AutoComplete component provides flexibility in binding both primitive data types and complex objects. To read the selected value at runtime, handle the change event and read the event.value property; for object binding, event.value returns the full selected object (not just fields.value).
Primitive data types
The React AutoComplete supports binding primitive values such as strings and numbers. You can bind local primitive arrays, fetch remote sources, or implement custom binding to suit specific requirements. Bind the value of primitive data to the value property of the React AutoComplete.
Primitive data types include:
- String
- Number
- Boolean
- Null
When the
valueproperty is set tonullor an empty string, the React AutoComplete clears any preselected item and renders with no value.
The following sample demonstrates setting a preselected value for primitive data types.
[Class Component]
import { AutoCompleteComponent } 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"];
}
value = "Item 5";
render() {
return (
// specifies the tag for render the AutoComplete component
<AutoCompleteComponent id="datas" dataSource={this.records} value={this.value} placeholder="e.g. Item 1" allowFiltering={true} popupHeight="200px">
</AutoCompleteComponent>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));import { AutoCompleteComponent } 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 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 AutoComplete component
<AutoCompleteComponent id="datas" dataSource={this.records} value={this.value} placeholder="e.g. Item 1" allowFiltering={true} popupHeight="200px">
</AutoCompleteComponent>
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));Object data types
In the React AutoComplete component, object binding allows you to bind to a dataset of objects. When allowObjectBinding is enabled (set to true), the value of the component will be an object of the same type as the selected item in the value property. When allowObjectBinding is enabled, set allowObjectBinding and the fields mapping before the value prop, so the component can resolve the object against the dataSource; the full selected object (not just fields.value) is returned in value. This feature binds arrays of objects, whether sourced locally or retrieved from remote endpoints, using the same approach shown below.
The following sample demonstrates setting a preselected value for object data types.
[Class Component]
import { AutoCompleteComponent } 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}`,
}));
}
value = { text: 'Item 5', value: 'id5'};
fields = { value: 'text' };
render() {
return (
// specifies the tag for render the AutoComplete component
<AutoCompleteComponent id="datas" dataSource={this.records} value={this.value} placeholder="e.g. Item 1" allowObjectBinding={true} fields={this.fields} allowFiltering={true} popupHeight="200px">
</AutoCompleteComponent>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));import { AutoCompleteComponent } 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 = { value: 'text' };
// 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 AutoComplete component
<AutoCompleteComponent id="datas" dataSource={this.records} value={this.value} placeholder="e.g. Item 1" allowObjectBinding={true} fields={this.fields} allowFiltering={true} popupHeight="200px">
</AutoCompleteComponent>
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));