-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
Adding items to an existing list will cause MatSelect to scroll to first item. #200
Comments
Can you please provide a stackblitz demo? |
I'm using ng-mat-select-infinite-scroll component and have noticed the same behavior. Here is working stackblitz If you remove ngx-mat-select-search from app.component.html and reload browser view on the right, options list will not jump to start after you scroll to the end and infinite scroller adds new items to it. If you leave ngx-mat-select-search as is in the app.component.html, every time you scroll to the end of options and infinite scroller loads a new batch of items, the list jumps to start. |
thanks for the example. ngx-mat-select-search/src/app/mat-select-search/mat-select-search.component.ts Lines 281 to 290 in 4138c75
this is needed to scroll to the first option of the filtered result. Somehow we should distinguish why the options changed, due to search or due to additional loading. |
a first fix for this could be to add an input to disable this behavior. @nprasovict @ArsalanSavand would you like to investigate this and find a fix? |
I won't have time to work on this since my team decided to go with different UX design where we don't use drop-down lists and hence won't be using ngx-mat-select-search and ng-mat-select-infinite-scroll. |
this issue is also reproducible in the demo by @L-NiNo: https://stackblitz.com/edit/mat-select-search-with-infinity-scroll |
@jvinhit would you like to give it a try and find a fix? |
This is the first time that I do a workaround like this, but I could be able to solve this by overwriting for a moment the setFirstItemActive function when you update your list, to avoid autoscrolling by doing <mat-form-field>
<mat-select [(value)]="itemSelected">
<mat-option>
<ngx-mat-select-search
#singleSelect
[formControl]="filterCtrl"
placeholderLabel="select an item">
<mat-icon ngxMatSelectSearchClear>x</mat-icon>
</ngx-mat-select-search>
</mat-option>
<mat-option
*ngFor="let option of options"
[value]="option._id">
{{option.name}}
</mat-option>
</mat-select>
</mat-form-field> import { MatSelect } from '@angular/material/select';
// other imports
@Component({
})
export class YourComponent implements OnInit, OnDestroy {
options: [];
@ViewChild('singleSelect', {static: false}) singleSelect: MatSelect;
constructor(/**/) {
// ...
}
updateList() {
// custom service request
this.service.request().subscribe(response) => {
// backuping in another variable setFirstItemActive fn
this.singleSelect.matSelect._keyManager.setFirstItemActivebk = this.singleSelect.matSelect._keyManager.setFirstItemActive;
this.singleSelect.matSelect._keyManager.setFirstItemActive = () => {};
this.options = this.options.concat(response['body']); // updating the options
// restoring setFirstItemActive fn
setTimeout(() => {
this.singleSelect.matSelect._keyManager..setFirstItemActive = this.singleSelect.matSelect._keyManager.setFirstItemActivebk;
}, 100);
})
}
} I hope someone could figure out a proper way to solve this, in the meanwhile maybe this solution can help someone :) |
I'm working on a fix to this. Essentially, you should only need to call |
Thank you |
I tried the solution to this issue and it didn't work. The solution given by |
@GreyM22 I just had this problem and I think I found a solution. Looks like the jump is triggered by ngx-mat-select-search/src/app/mat-select-search/mat-select-search.component.ts Lines 571 to 591 in 4138c75
This can be solved by calling: |
After setting property "disableScrollToActiveOnOptionsChanged" to true on ngx-mat-select-search problem is solved. |
I'm having a list of items and searching with server client.
I'm using a library to load more items once user reaches bottom of the scroll container. Problem is when I push new items to my list, it scrolls to first item.
The problem is not with the library I'm using, because even if I just randomly push an item to my list, it happens.
The text was updated successfully, but these errors were encountered: