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

Fixed when empty query added as bad query in case of no results #845

Open szabto opened 1 year ago

szabto commented 1 year ago

There was an issue when empty query was ran but no result given, the code did not even try to query result when empty search did not give results.

tkirda commented 1 year ago

Looks like that is by design, if there are no results, it would not try to query again. I don't see how it would not run a query. Did you set minChars to 0?

szabto commented 1 year ago

Sure I set it to minchars to 0. It works well, but there is a small bug.

My scenario is the following: You click into the textbox and shows suggestions without typing anything

Everything is fine, when you have previously typed stuff, so if you have results for no typed characters. And if you don't have suggestions for 0 characters, but there are results for typed characters, then it does not try to query these results.

Currently I implemented this as a city lister and selector. So I have previously selected cities when I type nothing. But when I start to type, I get city name results. But if I visit the page firstly, and I have no previously searched cities -> so 0 results, then it does nothing when I start to type.

tkirda commented 1 year ago

What you are modifying is processResponse. That means there was a query and it is processing response.

Maybe your problem is cache? Try disabling it and see how it works.

szabto commented 1 year ago

The actual problem is here:

       isBadQuery: function (q) {
            if (!this.options.preventBadQueries){
                return false;
            }

            var badQueries = this.badQueries,
                i = badQueries.length;

            while (i--) {
                if (q.indexOf(badQueries[i]) === 0) {
                    return true;
                }
            }

            return false;
        },

If you write an empty search, and it gives no result, then it won't try to query anything else because of this q.indexOf(badQueries[i]) === 0 As soon as it ran a query with "" and gives no result, then it goes to bad queries, then it won't try to query anything, because const str = "hello"; and str.indexOf("") will always be 0.

But maybe this is working as designed, and needs to be workarounded with always non empty result for "" search.