devbridge / jQuery-Autocomplete

Ajax Autocomplete for jQuery allows you to easily create autocomplete/autosuggest boxes for text input fields
https://www.devbridge.com/sourcery/components/jquery-autocomplete/
Other
3.56k stars 1.66k forks source link

fire onSelect, if only one result is returned #846

Closed svennd closed 1 year ago

svennd commented 1 year ago

If there is only a single suggestion, I would like to fire "onSelect". The reason : beside normal text, I also can receive (long) barcodes on the field. If this is the case, I don't need the extra interaction from the user.
autoSelectFirst : will select the first field but an enter is still required; (which is confusing since it might fire the entire form)

I tried with onSearchComplete, since this is a ajax call. But I can't (or don't know how) to fire onSelect function.

$('#autocomplete').autocomplete({

    serviceUrl: 'get_product',

    onSelect: function (suggestion) {
        console.log("onselect shot");
        var res = suggestion.data;
        $("#pid").val(res.id);
        // some more code
    },
    onSearchComplete: function (query, suggestion) { 

        if(suggestion.length == 1 && query.length > 20)
        {
            this.OnSelect(0); // this is invalid
        }
    },
    autoSelectFirst: true,
    minChars: '2'
});

I also tried with onHint, but then I have no idea when there is only 1 result. If I missed the obvious, sorry ! Thanks for this wonderful library !

tkirda commented 1 year ago

It will fire onSelect only if input value fully matches input. You may try this to trigger select manually:

$(this).autocomplete().select(0)