Autocomplete in core is limited when it comes to show long list as result, there are various ways to get the long list, I am not getting in to that..

The issue is that if we apply overflow: auto; to the following div, it gives us a vertical scroll, now this scroll works fine in FF, but in IE & Chrome when we try to scroll the list as soon as we click on the scroll the autocomplete result popup disappears.

I have a patch to fix this.. but first we need to reproduce this behavior, just apply any where in CSS

#autocomplete ul{
  overflow: auto;
  max-height: 100px;
}
#autocomplete li{
  white-space: pre-line;
}

after refreshing a page try to run the string search in autocomplete field in any browser and you will find that scrolls are not working in Chrome and IE.

to fix this behavior run the patch

diff --git a/misc/autocomplete.js b/misc/autocomplete.js
index 8f7ac60..b52d6af 100644
--- a/misc/autocomplete.js
+++ b/misc/autocomplete.js
@@ -22,6 +22,16 @@ Drupal.behaviors.autocomplete = {
         );
       new Drupal.jsAC($input, acdb[uri]);
     });
+
+    // fix autocomplete scrollbar issue
+    $('*').click(
+      function(event) { 
+        if (!$(this).children('div:first').is('#autocomplete') && Drupal.settings.autocompleteCurrent) {
+          Drupal.settings.autocompleteCurrent.hidePopup(); 
+          Drupal.settings.autocompleteCurrent.db.cancel(); 
+        }
+      }
+    );
   }
 };
 
@@ -47,7 +57,7 @@ Drupal.jsAC = function ($input, db) {
   $input
     .keydown(function (event) { return ac.onkeydown(this, event); })
     .keyup(function (event) { ac.onkeyup(this, event); })
-    .blur(function () { ac.hidePopup(); ac.db.cancel(); });
+    .blur(function () { Drupal.settings.autocompleteCurrent = ac; });
 
 };

Comments

xjm’s picture

Version: 7.15 » 8.x-dev
Priority: Major » Normal
Issue tags: +Novice, +Needs manual testing, +Needs backport to D7

Thanks Qandeel for the report.

This is not a major bug. I'm not actually sure it's a bug at all, since if I understand correctly, you have to add custom CSS that is not in core in order to trigger the bug? Demoting to normal, at least.

Let's have someone try reproducing this bug in D8 using the steps in the summary above, and confirm whether the autocomplete shows any problems normally. Please document exactly the steps you take to test it, starting from "Install Drupal core with the standard profile."

seutje’s picture

not a big fan of the shorthand methods (click, load, ...), but autocomplete in D7 is full of em, would still prefer .bind('click' (or .on('click' in 8) though
also, it's probably better to bind to document (or body) and use event.target instead of this

iflista’s picture

Status: Active » Closed (works as designed)

Drupal 8 Testing
I have tested Chrome, Firefox and Safari behavior for drupal 8 and it's the same in every browser. I suppose that 4 fields for auto-complete is default drupal's behavior and it's also not a bug.

My observation:

  • I created 17 taxonomy terms variations all starting from letter a
  • then I tried to autocomplete starting from a
  • It showed 4 autocomplete options in Firefox, Chrome and Safari
  • When I tried to add letters, for example for asd it showed:
  1. adfsdfa
  2. adsfasdf
  3. asd
  4. asdfads

As you can see there is a problem but not the same as in issue description. Logically the last number showed should be 1-st or second but not the last.

Drupal 7 Testing
Testing was made with different taxonomies but still the same behavior, the most obvious variants were last or in the middle of list.

The only difference between 2 versions is that d8 shows 4 variants and less, and d7 - 5 variants and less.

I found issue that tries to fix described behavior in this comment #1120144: Term Reference Autocomplete widget - add option to match only start of term and expose settings (size, match)

And also to indicate that more results were found than displayed #100727: Autocomplete should indicate that more results were found than displayed

After applying patch for d7 - nothing changed. Same behavior.

What has been used for testing:
OS Mountain Lion
Firefox 16.0.1
Safari 6.0.1
Chrome 22.0.1229.94

iflista’s picture

I should point the way I made testing.

  1. Tested Drupal 8 without css from summary applied.
  2. Tested Drupal 7 without css applied, only installed theme - Garland
  3. Tested Drupal 7 with css applied, theme - Garland
  4. Tested Drupal 7 with css and patch applied, theme - Garland

No difference in drupal's behaviour at all.
So we need steps to reproduce issue from @Qandeel, or this is closed issue.

qandeel’s picture

I have not tested this in D8, but when you add a CSS change and add overflow:auto; and if you trim the height of the autocomplete drop down which is:

#autocomplete ul{
  overflow: auto;
  max-height: 100px;
}
#autocomplete li{
  white-space: pre-line;
}

Then try to search a substring that occurs more than 6 items.. so that you can see the scroll bars on that autocomplete drop down.

I am talking about the scroll on this drop down that it appears on all the browsers but using mouse the scroll functionality only works on FF but not on Chrome and IE. In chrome and IE as soon as you click on the scroll bar to hold and scroll .. the drop down gets disappeared.

shabana.navas’s picture

Version: 8.x-dev » 7.x-dev
Status: Closed (works as designed) » Active

I am facing the same exact problem and I am running Drupal 7, version 7.19. This issue shouldn't be taken lightly as the autocomplete is a core part of Drupal and it should work in all browsers. I have tested it with Firefox, Chrome and IE. If we limit the autocomplete size, then, the scrollbar works perfectly in FF, but just as Qandeel said, it disappears when we click the scroll bar in both Chrome and IE.

I hope this patch will go into the next core update.

samvel’s picture

Status: Active » Needs review

So, need change status and apply

Sylense’s picture

Confirming that this works for me. I applied the patch and CSS from the OP and it now works for me in both IE and Chrome

Sylense’s picture

Issue summary: View changes

patch also added in comment as text

shameemkm’s picture

Issue summary: View changes
StatusFileSize
new1.31 KB

Back port of the patch for D7 the patch is format-patch

qandeel’s picture

@shameemkm I dont get it what is the difference between the patch you created and the one that I already there??

shameemkm’s picture

@Qandeel

I was just creating a backport to Drupal 7. Looks like both are same. Was your patch for 7 or 8?

shameemkm’s picture

@Qandeel,

Looks like you had created a D7 patch. My mistake. My patch can be ignored or deleted.

mgifford’s picture

qandeel’s picture

@mgifford, I dont know if I will be credited for this patch, I submitted original patch which is 100% same as the latest one only the difference is that some one just tried to name it with his own user.. In opensource its a crime to steal some one else work and put a name on it.

nod_’s picture

Status: Needs review » Needs work

Well, in any case, $('*').click() is not ok. Drupal likes deep nested structures, this will kill performance. Use delegate or another solution.

Now you can both collaborate on finding a core-worthy solution to the issue, no need to quarrel about attribution :D

fubhy’s picture

Well, in any case, $('*').click() is not ok. Drupal likes deep nested structures, this will kill performance. Use delegate or another solution.

Yes, wildcard selectors are not a solution. Ever.

Instead, this is actually pretty simple to fix afaik by using a little trick. It should work by simply setting the tabindex on #autocomplete to a negative value:

#autocomplete {
  max-height: 200px;
  overflow-y: auto;
  tabindex: -1;
}

That *should* work.

mrdalesmith’s picture

The suggestion in #16 isn't working for me. Anyone else got it to work?

mrdalesmith’s picture

StatusFileSize
new1.07 KB

Ok, JQuery isn't one of my strong points, so I'm going to upload an amendment to the original patch so someone can tell me why it's a bad idea :)

acabouet’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 18: autocomplete-overflow-1809580-18.patch, failed testing.

mrdalesmith’s picture

Status: Needs work » Needs review
StatusFileSize
new1.09 KB

Think that patch fail is folder structure: still not 100% clear on how a patch should be formatted. Let's try again.

crmn’s picture

StatusFileSize
new999 bytes

i had a project where the parent wasn't a div, so the patch above didn't fixed the issue for me. added a patch not addressing exactly a div.

ruscoe’s picture

StatusFileSize
new1.12 KB

The patch in #21 solves the problem, but the extra call to hidePopup() occasionally causes the autocomplete text field to be populated with "undefined" due to missing autocomplete data. This patch fixes that by setting the last selected element to false, allowing hidePopup() to hide the popup without attempting to add anything to the text field.

ruscoe’s picture

crmn’s picture

is there a reason why the parent has to be a div again or did you simply just worked with patch #21?
patch #23 does not work for me because i have an example where the parent is a td instead of a div. (see #22)

ruscoe’s picture

StatusFileSize
new1.12 KB

New patch to include more versatile solution from patch in #22. Thanks to @crmn.

mgifford’s picture

What are the steps to replicate this error?

mgifford’s picture

What are the steps to replicate this error?

zalak.addweb’s picture

Issue summary: View changes

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.