Random Quote

Experience is what you get when you didn’t get what you wanted. And experience is often the most valuable thing you have to offer

— Randy Pausch, The Last Lecture

Stack Exchange

profile for Andrea Girardi on Stack Exchange, a network of free, community-driven Q&A sites


React.js: fill options of Autocomplete with API results

The autocomplete is a normal text input enhanced by a panel of suggested options.  Autocomplete is a feature that helps in predicting the rest of the word typed by a user. Autocomplete is helpful from the user as well as the user experience perspective. It makes users happy by saving their time and also by offering them several choices.

In this case, I fill the Autocomplete with the results of Google Ads locations an this is the expected result:

For first, import Autocomplete component (of course must be installed):

import Autocomplete from '@material-ui/lab/Autocomplete';
// or
import { Autocomplete } from '@material-ui/lab';

Define the object that will be used to collect the Autocomplete options:

const locationResults = [];

This is how Autocomplete is defined:

let autocompleteBox = <Grid item xs={12} md={12} sm={12} lg={12}>
        getOptionLabel={option => option.locationName}      
        style={{ width: 600, marginTop: 20 }}                        
        onChange={(event, autocompleteValue) => this.setState({ autocompleteValue })}
        renderInput={params => <TextField {...params} label="Search location" variant="outlined" onChange={this.handleAutocompleteTextChange.bind(this)} />}


Once the user enters some text on Search location TextField, this function is called (I expect user inserts at least 3 chars before call the API):

 * On change text of Autocomplete
handleAutocompleteTextChange = (event) => {

        query: event.target.value
    }, () => {

        if (this.state.query && this.state.query.length > 3) {

This is how the function getLocations() is implemented:

 * API Function to retrieve locations
getLocations = (locationQuery) => {

            method : 'get', url: GET_LOCATIONS, auth: this.state.userInfo.apiAuth, params: {
            user: this.state.userInfo.username,
            customerId: this.state.clientCustomerId,
            query: locationQuery
    }).then(res => {

        if (res.status == 200) {
            this.setState({results: res},()=>{
        } else {
            this.setState({results: []});

    }).catch(error => {
        this.setState({results: []});


The last thing is to update the options on Autocomplete:

 * Reload the results after API call
forceReloadOrganization = (results) => {

        if ( results.data != "" ) {
            results.data.map(item => {

                        type: item.location.displayType,
                        locationName:item.canonicalName + ": " + item.location.displayType + " (" + item.location.id + ")"


Make jquery function run on page load

Load jQuery when the HTML document is loaded:

$(document).ready(function() {
	// business code

Load jQuery when all the images on a page have also loaded:

$(window).load(function() {
    // business code

Change Eclipse settings to ignore jquery errors on a specific file

In my dynamic web project in eclipse, I have jQuery in my js source folder. For some reason, Eclipse is not handling it correctly and interpreting many lines as errors in the standard jQuery file (even though I have the javascript development tools installed).

I have found that I can leave the JavaScript Validator enable and ignore specific files by adding a suitable exclusion pattern e.g. **/jquery*.js to the JavaScript/Include Path/Source/Excluded group (Project->Properties->JavaScript->Include Path->Source).