It has been days that I am trying to fix some angular 15 code but I can't find the solution. I am using a mat-select(Legacy) with ngx-mat-select-search and virtual scroll cdk-virtual-scroll-viewport. When I select a value, it is displayed, but when I search for some value, it's not displayed but it's read, I can display it in my console but not in my mat-select.
I am facing the same problem in this stackblitz link, you can search for "Bank C" and select it, it's displayed, but when you search for "Bank Q" and select it, the value is not displayed.
Here's my html code :
<mat-form-field>
<mat-select [formControl]="formControl"
[placeholder]="placeholder"
[(ngModel)]="values"
[matTooltip]="getTooltip(values)"
[multiple]="multiple"
[required]="required"
(openedChange)="openChange($event)">
<mat-option *ngIf="!hideSearch">
<ngx-mat-select-search
[placeholderLabel]="filterItem"
[noEntriesFoundLabel]="noItemMatch"
[formControl]="filterItemCtrl"
[showToggleAllCheckbox]="showAllCheckbox"
[toggleAllCheckboxChecked]="showAllCheckbox && multiple && items.length && values.length === items.length"
(toggleAll)="checkAllItems($event)">
</ngx-mat-select-search>
</mat-option>
<mat-option *ngIf="emptyItem">{{ emptyItem }}</mat-option>
<cdk-virtual-scroll-viewport [itemSize]="5" [style.height.px]=viewPortHeightPx class="custom-viewport">
<mat-option *cdkVirtualFor="let item of filteredItems | async;"
[value]="item"
(onSelectionChange)="clickOption($event)">
<div >
{{ item }}
</div>
</mat-option>
</cdk-virtual-scroll-viewport>
</mat-select>
Thanks for helping me.
This one was really tough to debug, but basically the problem was that when an
mat-optionis outside the rendering window of the virtual scroll the option will not get selected, so I wrote a special slice method, that will always place the selected bank on the top of the array, so that will get surely selected and that fixed your issue.Above code creates the slice with the selected value in the beginning!
html
ts
stackblitz