0

cdk-virtual-scroll-viewport is not working properly when I use more than two cdk-virtual-scroll-viewport in the same page.

Stackblitz example

In the above example, first dropdown is working properly. but in the second dropdown, getting blank space after scrolling the dropdown and open the dropdown again. enter image description here

    <mat-select [formControl]="multiSelectControl"  (openedChange)="openChange($event)"
        placeholder="Select">
        <cdk-virtual-scroll-viewport itemSize="50" [style.height.px]=5*48 >
            <mat-option *cdkVirtualFor="let topping of toppingList" [value]="topping"
                >{{topping.viewValue}}</mat-option>
        </cdk-virtual-scroll-viewport>
    </mat-select>
</mat-form-field>

<br>
<br>

<mat-form-field>
<mat-select [formControl]="multiSelectControl1"  (openedChange)="openChange1($event)"
    placeholder="Select">
    <cdk-virtual-scroll-viewport itemSize="50" [style.height.px]=5*48 minBufferPx="200" maxBufferPx="400">
        <mat-option *cdkVirtualFor="let topping of toppingList1" [value]="topping"
            >{{topping.viewValue}}</mat-option>
    </cdk-virtual-scroll-viewport>
</mat-select>
</mat-form-field>

export class AppComponent {

  toppingList: any[] = [];
  toppingList1: any[] = [];

  @ViewChild(CdkVirtualScrollViewport, { static: true })
  cdkVirtualScrollViewPort: CdkVirtualScrollViewport;

  @ViewChild(CdkVirtualScrollViewport, { static: true })
  cdkVirtualScrollViewPort1: CdkVirtualScrollViewport;

  multiSelectControl = new FormControl();
  multiSelectControl1 = new FormControl();

  constructor(private cd: ChangeDetectorRef, readonly sd: ScrollDispatcher) {
    for (let i = 0; i < 100000; i++) {
      this.toppingList.push({ id: i, viewValue: "option-" + i });
      this.toppingList1.push({ id: i, viewValue: "option-" + i });
    }
  }

  openChange($event: boolean) {
    if ($event) {
      this.cdkVirtualScrollViewPort.scrollToIndex(0);
      this.cdkVirtualScrollViewPort.checkViewportSize();
    }
  }

  openChange1($event: boolean) {
    if ($event) {
      this.cdkVirtualScrollViewPort1.scrollToIndex(0);
      this.cdkVirtualScrollViewPort1.checkViewportSize();
      
    }
  }


}


this.cdkVirtualScrollViewPort1.scrollToIndex(0); scrolling to a particular index of second dropdown is also not working.

Any suggestions would be much appreciated!

2
  • a stackblitz is nice but please include minimal code example here also. Links can break Commented Jan 9, 2023 at 10:39
  • added minimal codes Commented Jan 10, 2023 at 11:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.