Does any one met the issue of angular 7 cdk virtual scrolling working abnormally in mat-tab group.
https://github.com/angular/material2/issues/13981
My city component template looks like that
<cdk-virtual-scroll-viewport class="list-container" [itemSize]="50" minBufferPx="200" maxBufferPx="400" (scrolledIndexChange)="getNextBatch($event)">
<div *cdkVirtualFor="let state of statesObservable | async; trackBy: trackByFn" class="list-group-item state-item">
<div class="state">{{state.name}}</div>
<div class="capital">{{state.capital}}</div>
</div>
</cdk-virtual-scroll-viewport>
When put this city component to the mat-tab-group as the second tab
<mat-tab-group>
<mat-tab label="Country">
<app-country></app-country>
</mat-tab>
<mat-tab label="City">
<app-city></app-city>
</mat-tab>
</mat-tab-group>
The result looks like below:
The stackblitz code is here: https://stackblitz.com/edit/angular7-virtual-scroll-issue
Does anyone have some idea for this issue?
This feature is added to CDK (Component Development Kit). Virtual scrolling shows up the visible dom elements to the user, as the user scrolls, the next list is displayed. This gives faster experience as the full list is not loaded at one go and only loaded as per the visibility on the screen.
Loading hundreds of elements can be slow in any browser; virtual scrolling enables a performant way to simulate all items being rendered by making the height of the container element the same as the height of total number of elements to be rendered, and then only rendering the items in view.
[itemSize] dictates how tall in pixels each row in the list is. The virtual scroller then uses this (in part) to determine how many rows it can buffer above and below the viewport. The less tall you make the itemSize , the more it will try to load and buffer.
You tell it to keep the Buffer size between 200px and 400px, yet your scroll window is much taller than that.
Change both the minimum and maximum to 1200px, this will keep the items cover your viewport, even when you scroll down for more items.
https://stackblitz.com/edit/angular7-virtual-scroll-issue-ftcnng
The question has been asked some time ago, but there is a solution which is not a workaround.
First you need the Viewport Reference:
@ViewChild(CdkVirtualScrollViewport, {static: false}) cdkVirtualScrollViewport: CdkVirtualScrollViewport;
Then you can call the Method checkViewportSize()
when the size of the Viewport has changed. Before you call this method you need to update the style height of the Viewport container.
this.heightChange$.subscribe(() => {
this.cdkVirtualScrollViewport.checkViewportSize();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With