Alex
Alex

Reputation: 68416

jQuery - autocomplete plugin

I'm using this one:

https://github.com/agarzola/jQueryAutocompletePlugin

The only problem is that when you type the 1st character in the input field, it will display all term suggestions, including terms which don't contain this character.

And only after you type the 2nd character it will behave normally, displaying tag suggestions containing only those 2 characters.

I'm using this configuration:

$('input.autocomplete').autocomplete("localhost/boo/?get_suggestions=1", {
   width: 250,
   max: 100,
   matchContains: true,
   minChars: 1,
   selectFirst: true,
   cache: false,
   multiple: true,
   multipleSeparator: " "
 });

Does anyone know a workaround for this?

alt text

Also when I type a random string, which I know it's not in the list, for eg. *&@FGBHFHBOFUBF*UB# it will display the entire list again :(

The back-end:

if($_GET['get_suggestions']):
  $terms = get_all_terms();
  foreach ($terms as $term) echo $term['title']."\n";
  die();
endif;

Upvotes: 2

Views: 1289

Answers (4)

Alfonso
Alfonso

Reputation: 2296

I’m the one maintaining that github repo. Here’s what’s going on:

In the case of using a url for the data, the script sends the request as whatever you set in the specified url, and adds q=[current input value] at the end. In the case of initial load when you type in “a”, this is sent to your backend script: localhost/boo/?get_suggestions=1&q=a. Thus, autocompelte.js expects this initial query to produce only items that match the query. After that initial request, the script will take on the filtering subsets internally, to decrease server load. This explains why “ac” returns only items that match your criteria. This is the autocomplete script doing its job of filtering what the server gave it.

If I’m interpreting your backend code correctly, it makes no use whatsoever of the q parameter being sent in the request, so your code is returning every possible term. Autocomplete presumes this is the result of a proper search and shows you all of it, waiting for more characters to be typed in for it to filter the list further.

The point being that you need to make your backend script filter the list of terms to whatever matches the q parameter before returning it to the autocomplete script.

Let me know if I can be of further assistance!

Upvotes: 1

Ast Derek
Ast Derek

Reputation: 2729

That autocomplete code is wrong at some part. From the image you posted, when you typed a a long list of all terms showed; when you typed ac the list of suggestions matched a instead of ac.

What does it mean? the code takes the input value before the new character is taken into account. You may take a dive into the plugin code, or use a new plugin.

Upvotes: 1

Jason
Jason

Reputation: 15358

Change your minchars to 2

 minChars: 2

That will make it so it only suggests things from the 2 character.

or try turning off multiple.

multiple: false

Do you need that to be enabled?

If that's not your cup of tea, post the code for localhost/boo/?get_suggestions=1 and we will take a look see :)

Upvotes: 1

orlp
orlp

Reputation: 117681

I would suggest disabling first character suggestions altogether, and start on the second or even third character. Most people including myself find it annoying to instantly get spammed with suggestions after typing a "b".

Upvotes: 1

Related Questions