Using Devbridge Autocomplete, is there a way to detect no results?

3.3k Views Asked by At

I'm using Devbridge Autocomplete (https://www.devbridge.com/sourcery/components/jquery-autocomplete) on an input field type text and reading results from an array.

Is there a way to:

1) Only allow to select from the list of results and not type anything else in the field

2) If no results are found, trigger a function

Code:

var people = ['Ross Chapman', 'Curtis Gomez', 'Dolores Gonzales'];

$('.people').autocomplete({
    lookup: people
});
1

There are 1 best solutions below

1
On BEST ANSWER

You can use onSearchComplete method as mentioned in the docs here.

onSearchComplete: function (query, suggestions) {}

second parameter suggestions is an array of suggestions as letters are being typed in the input. So by checking suggestions.length we can catch the condition when the suggestions array is empty, meaning no results found.

var people = ['Ross Chapman', 'Curtis Gomez', 'Dolores Gonzales'];

$('.people').autocomplete({
    lookup: people,
    onSearchComplete: function (query, suggestions) {
        if(!suggestions.length){
            console.log('no suggestion');
        }
    }
});

Here is a demo http://jsfiddle.net/dhirajbodicherla/PSJTQ/21/