angularangular-materialvirtualscrollangular-cdk-virtual-scroll

Angular Material Scroll loads more items than expected


I am trying to load 20 items through the virtual scroll component, but I see 34 as soon as the page loads, without doing any scrolling at all. Have I got the css wrong? Or there is some setting on the control that I forgot? The project is located here.


Solution

  • This tutorial on Firebase says:

    Let’s start by reviewing a few important concepts with virtual scroll. First, you declare the cdk-virtual-scroll-viewport component to provide a context for virtual scrolling. It should have an itemSize input property defined as the pixel height of each item.

    You can see that this is not made especially for grid scroll views, but we can work around this limitation:

    You have 5 columns of images each 160 pixels high plus margins top and bottom each 10 pixels, yielding 180 pixels.

    Now we calculate 180 divided by 5 and get 36. This is the itemSize you need.

    <cdk-virtual-scroll-viewport itemSize="36">
      <div class="image" *cdkVirtualFor="let image of images">
        <a (click)="viewDetail(image)">
          <img src="https://picsum.photos/200/160/?image={{ image.id }}" />
        </a>
      </div>
    </cdk-virtual-scroll-viewport>
    

    See this fork of your StackBlitz

    The official documentation is here: Angular | Scrolling

    You could change the buffer sizes as well

    <cdk-virtual-scroll-viewport itemSize="36" minBufferPx="540" maxBufferPx="540">
      <!-- ... -->
    </cdk-virtual-scroll-viewport>
    

    See: Angular | Scrolling # Scrolling over fixed size items

    You can also let Angular do the math:

    <cdk-virtual-scroll-viewport itemSize="{{ 180 / 5 }}" minBufferPx="{{ 180 * 3 }}" maxBufferPx="{{ 180 * 3 * 3 }}">
    

    That means that you will load three pages (3 rows per page) in advance and load more if only one page buffer (3 rows of images per page) is left