-
Notifications
You must be signed in to change notification settings - Fork 6.8k
autocomplete does not stick when scrolling #10079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
That's because, by default, Material won't listen to scroll events on all elements. Is your main scroll container something different than the |
@crisbeto yes, the scroll container is actually mat-sidenav-content element, descendant of the body element, which has |
Usually you'd have to add the |
I have noticed that the mat-select does not have this issue, as it blocks scroll action when the panel is opened. May be autocomplete should follow the same approach. |
Having same use case as @weijyewang with |
Having same issue when the main scroll container is content of dialog popup (mat-dialog-content). Ideally it should work similar to mat-select. |
I was with the same problem, I used the workaround described in this issue: |
@israelpereira #7897 did help. On using Reproduced on stackblitz. Click on the 'open popup' button and scroll the dialog section. I think the ideal solution would be to disable scroll when the dropdown is opened as suggested by @Karankang007 in comment. |
I had the same issue.. to solve it.. I found something called ScrollStrategy and I used the following code:
It closes the autocomplete box when it identifies the scroll outside the autocomplete. hope it helps. |
thanks @israelpereira. this helps. The dropdown overlay closes on scroll. The only side effect is that to open the dropdown overlay once again, the focus has to be taken out of the autocomplete input and back into it. Behaviour similar to mat-select could be achieved if |
Welcome, I had this issue too...
add this code to the component and put the (click)="openAutocompletePanel()" in the input field, it will solves the problem. I don't know if it is the best solution, probably not, but worked .. I was losing so much time on it.. |
@israelpereira thanks for |
Solution:
Locate the Modal that has this component. Place the cdkScrollable directive on the outer most div of that Modal... ie:
This worked for me. |
just add below code to your autocomplete function and it will work appendTo: $('#tag').parent(), this will stick the autocomplete list to its parent textbox |
Is there any solution for mat-autocomplete not sticking to the input when scrolled. Note: None of the given solution worked for me |
@omaracrystal solution work for me, but then , i have problem with z-index. |
I'm having the same issue when I used autocomplete inside mat-side-nav |
Hey @weijyewang, @fzs1994 I'm in the same scenario, did you manage to solve it ? None of the given solution worked for me. |
@matiasfs12 I just added |
That doesn't work for me since i'm not using mat-sidenav-container, nor mat-sidenav-content, actually i'm putting the cdkScrollable on mat-sidenav tag, but the scroll listener it's not even firing up because there is an intermediate element mat-drawer-inner-container which actually scroll up/down the content, and i cannot disable the scroll in this element with CSS. In any case, i will try to do a stackblitz, thanks in advance! |
@matiasfs12 you just have to set |
Hey @weijyewang , Finally managed to get it working, I had to append a |
All the above solutions not worked for me, Please anyone has the best solution. |
The above solution works. you just have to find the class where you are defining the overflow property to scroll this will give you the element which is controlling the scrolling of the page. Then add cdkScrollable to that element. And add this in your module. since ScrollDispatchModule has been renamed to ScrollingModule. |
Nothing worked for me. |
@panyann If you have a autocomplete within a mat dialog - this is how I solved this issue.
|
@omaracrystal I don't have a dialog and cdkScrollable seems to not work whatever I do! First of all we need to be able to use autoComplete methods, so we must take this control from the view. Add the id: #autoCompleteInput
In the component:
Now we have autoComplete as a variable. Next we need a scrolling event:
And finally add a function to the component:
You can set it to close the panel or update its position. Problem solved, but is not perfect if you have a lot of these autoComplete elements. I can only wonder why this position update does not happen automatically and we all need to make idiots out of ourselves and waste time for finding solution... |
Thank you @panyann This is exactly what I wanted |
@panyann Thanks a lot! your solution worked 💯 . 🥇
|
For @matiasfs12 and others with overlays in mat-sidenav or drawer, CdkScrollable is declared on mat-sidenav-container but not mat-sidenav. Additionally, as was pointed out mat-sidenav has an inner div element which handles overflow and can't be accessed. Place the following in your global styles, and declare CdkScrollable on the mat-sidenav. .mat-drawer, .mat-sidenav {
&[cdk-scrollable], &[cdkScrollable] {
.mat-drawer-inner-container {
overflow: visible;
}
}
} This issue affects more than autocomplete, so I submitted a separate issue #19846. There is a similar issue with tabs #8405. |
Thank You @TrevorKarjanis |
@TrevorKarjanis grate man... it is a very excellent and proper solution... |
I've transformed this into a directive so you don't have to include this code in every component:
In your input element, include these last two attributes (#trigger and [autocompletePosition] to activate the directive: And yes, I also wonder why this is not fixed in the library itself. There are a lot of bug reports about this and sprinkling cdkScrollable over your components does not look like a good pattern to me... |
@NithinBiliya Any luck in solving the issue i.e auto-select goes out of the scrollable area? |
Hi everyone, I also have the issue of autocomplete drop down issue while scrolling. I do within the rails application. Kindly suggest me the best solution for this!! |
With @dbitkovski we came up to slightly different solution for directive that @janvanrossum-bookzo mentioned. Instead of passing trigger trough the input we just take it from DI. Also we apply this directive to all
|
I've created a work around using (scroll) app.component.html -
app.component.ts -
This would update the top or bottom of the currently opened This is working for select and date picker as well |
Para solucionar el problema de autocomplete superpuesto a otros elementos, mejor es que al detectar el scroll este se cierre, y asi se evitara este encima de los demas elementos cuando este dentro de un element con scroll, entonces para eso aplicaria lo mismo que @pavel-romanov8 con una ligera modificacion para cerrarlo cuando se detecte scroll, obviando el scroll del autocomplete, espero le sirva a alguien. import { Directive, OnDestroy } from '@angular/core';
import { MatAutocompleteTrigger } from '@angular/material/autocomplete';
@Directive({
selector: '[appAutocompletePosition]'
})
export class AutocompletePositionDirective implements OnDestroy {
public constructor(private readonly matAutocompleteTrigger: MatAutocompleteTrigger) {
window.addEventListener('scroll', this.scrollParent.bind(this), true);
}
public ngOnDestroy(): void {
window.removeEventListener('scroll', this.scrollParent, true);
}
private scrollParent(event: Event) {
if (event.target != document.getElementsByClassName('mat-autocomplete-panel')[0]) {
if (this.matAutocompleteTrigger == null) {
return;
}
if (this.matAutocompleteTrigger.panelOpen) {
this.matAutocompleteTrigger.closePanel();
}
}
}
} |
I can confirm this works. however, when used inside a mat-table it does not work. or is there a different way to do so? |
@chitgoks Sorry, I can't help you, but for sure there is some workaround, like always. The real question is why it's not fixed after more than 5 years of reporting the issue... |
Agreed - this sort of lackadaisical response to fairly important issues (where UI/X is involved) cramps my style. And honsestly, the grass is just as brown in any other yard... I've begun migrating to custom controls from stock. Once I started to saw away at the umbilical, I realized Angular already has all I need to make some great controls. Then any issues are mine and I can resolve them at the root level instead of overrides or other libraries to plug the holes. |
I got this to work in the mat-table. I made it as another custom component using that solution. |
thanks @panyann |
I am also facing the same issue when I try to open a mat-autocomplete dropdown inside a mat-dialog. Dropdown is not sticking to the input. Any solution for this? |
Bug, feature request, or proposal:
Bug
What is the expected behavior?
The autocomplete drop down list should stick to the bottom of input element when scrolling
What is the current behavior?
The drop down list is sticky to position on the screen, it will not follow the input element position when scrolling
What are the steps to reproduce?
What is the use-case or motivation for changing an existing behavior?
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Windows 10 64 bit
Chrome Version 64.0.3282.167 (Official Build) (64-bit)
Angular 5.1.2
Angular Material 5.0.2
TypeScript 2.7.1
Is there anything else we should know?
The text was updated successfully, but these errors were encountered: