Having Issues with CDK Virtual Scrolling in Angular Material Table

Dealing with an angular material table that contains millions of records can be quite challenging. I have implemented pagination with various options such as 10, 25, 50, 100, 500, and 1000 items per page. However, when selecting the option for 1000 or all records, there is a noticeable delay in updating the pagination interface and loading new data while scrolling through the table.

To address this issue and improve performance, I decided to implement CDK virtual scroll for the table. After adding the virtual scroll functionality, I encountered a problem where the table was not loading properly. Below is a snippet of the code:

<cdk-virtual-scroll-viewport itemSize="50">
    <table mat-table fixedLayout [dataSource]="dataSource" multiTemplateDataRows matSort (matSortChange)="sortData($event)" #sort="matSort" matSortStart="desc">
        <ng-container *ngFor="let column of columns;" [matColumnDef]="column.columnDef">
            <ng-container>
                <th mat-header-cell *matHeaderCellDef mat-sort-header> {{column.header}} </th>
                <td mat-cell *matCellDef="let element">
                    {{element[column.columnDef]}}
                </td>
            </ng-container>
            ................
        </ng-container>
        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
        <tr class="mat-row" *matNoDataRow>
            <td class="mat-cell" colspan="4">No records found.</td>
        </tr>
    </table>
</cdk-virtual-scroll-viewport>
<mat-paginator #paginator="matPaginator" [pageSizeOptions]="pageSizeOptions" showFirstLastButtons (page)="onPaginateChange($event)">
</mat-paginator>

Even after attempting to replace *ngFor with *cdkVirtualFor, the issue persisted. Any guidance or assistance on resolving this matter would be greatly appreciated.

Thank you.

Answer №1

You forgot to specify the height and should use *cdkVirtualFor instead of *ngFor

<cdk-virtual-scroll-viewport itemSize="50" style="height:500px;">
            <table mat-table fixedLayout [dataSource]="dataSource" multiTemplateDataRows matSort
                (matSortChange)="sortData($event)" #sort="matSort" matSortStart="desc">
                <ng-container *cdkVirtualFor="let column of columns;" [matColumnDef]="column.columnDef">
                    <ng-container>
                        <th mat-header-cell *matHeaderCellDef mat-sort-header> {{column.header}} </th>
                        <td mat-cell *matCellDef="let element">
                            {{element[column.columnDef]}}
                        </td>
                    </ng-container>
            ................
                </ng-container>
                <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
                <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
                <tr class="mat-row" *matNoDataRow>
                    <td class="mat-cell" colspan="4">No records found.</td>
                </tr>
            </table>
        </cdk-virtual-scroll-viewport>

Answer №2

mat-table currently does not have the capability to render rows with virtual scroll. However, you can create a simple <table> within a

<cdk-virtual-scroll-viewport>
element using <tr *cdkVirtualFor> and customize its styling to align with the Material design principles.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mastering the art of navigating through intricate nested properties within JSON data structures

Presented below is a dynamic JSON structure: data = { "name": "deltha", "type": "object", "important": [ "name", "id", "number" ], "information": { "place": { "editable": false, "visible": true }, "info": { ...

The type of 'username' cannot be determined without specifying the reference to '@angular/forms/forms' in the node modules

I'm encountering an issue with my application: forgot-password.component.ts(44,7): error TS2742: The inferred type of 'username' cannot be named without a reference to '.../node_modules/@angular/forms/forms'. This is likely not po ...

Leverage a custom server (such as NestJS) within NextJS to dynamically render targeted pages

I am experimenting with using NestJS as a custom server for NextJS, following the instructions in this article. Here is a simplified version of the code: @Controller('/') export class ViewController { @Get('*') async static(@Req() r ...

Issue encountered while combining TypeScript interface function declarations (Interface Merging)

interface Cat { meow(words: string): string; } interface Cat { meow(num: number): number; } const cat: Cat = { meow(wordsOrNum): string | number { return wordsOrNum; } } In the example code above, interfaces demonstrate how decla ...

Angular i18n simplifies the process of internationalizing and local

Is there a way to display different labels based on the user without using conditionals? I want to maintain consistency in this large project. For example: When User 1 is logged in: <h1>Hello, I am User 1</h1> When User 2 is logged in: < ...

What causes an error when the App is enclosed within a Provider component?

What is causing the error when wrapping the App in Provider? Are there any additional changes or additions needed? Error: "could not find react-redux context value; please ensure the component is wrapped in a @Provider@" import React from ' ...

Styling pagination using CSS and jQuery

I am looking to display 3 sections in a simple, paginated way that mimics tabs. I am trying to implement the logic where when a pagination item is clicked and has the 'active' class, it should show the corresponding block. However, I am strugglin ...

What is the best way to integrate Tawk.to into a React application while using typescript?

Having some issues integrating tawk.to into my website built with React and TypeScript. I have installed their official npm package, but encountered an error message: import TawkMessengerReact from '@tawk.to/tawk-messenger-react'; Could not fin ...

What could be causing my component to not refresh when used as a child?

I have been experimenting with some code to track rerenders. The initial approach failed when passing <MyComponent> as a child component. it("should return the same object after parent component rerenders", async () => { jest.useF ...

What is the best way to dynamically disable choices in mat-select depending on the option chosen?

I was recently working on a project that involved using mat-select elements. In this project, I encountered a specific requirement where I needed to achieve the following two tasks: When the 'all' option is selected in either of the mat-select e ...

In a Custom Next.js App component, React props do not cascade down

I recently developed a custom next.js App component as a class with the purpose of overriding the componentDidMount function to initialize Google Analytics. class MyApp extends App { async componentDidMount(): Promise<void> { await initia ...

Angular 6 and above: The use of ProvidedIn in a submodule is leading to a circular dependency issue

A resolve service is being implemented using the new providedIn attribute. This translations resolver is utilized in a protected module: import { Injectable } from '@angular/core'; import { Observable , pipe } from 'rxjs'; import { ...

Navigating through a JSON dictionary in Svelte: A step-by-step guide

When working with Svelte, the #each construct allows for easy iteration over array-like objects. But what if you have a JSON dictionary object instead? Is there a way in Svelte to iterate over this type of object in order to populate a dropdown menu? The ...

What is the process for recording soft-navigation modifications in mPulse Boomerang for an Angular application?

I have been trying to use the BOOMR.plugins.Angular plugin for AngularJS 1.x, but I have not been able to find an example or plugin that works with Angular. After reviewing the documentation, I have come up with the following solution: @Injectable() export ...

When working with Typescript and Vue.js, it's important to ensure that properties are initialized before

Check out the following code snippet: export default class PrimitiveLink extends Vue { style = { // Reset display: 'inline-block', textDecoration: 'none', outline: 'none', // Theme ...this.themeStyle ...

Jasmine is raising an error: "TypeError: Unable to access the property 'client' of an undefined object"

While running test cases for the EditFlag component in Angular, I encountered an error stating TypeError: Cannot read property 'client' of undefined. Additionally, I am looking to add a test case for a switch case function. Can someone assist me ...

React error: The module "react-router-dom" does not have a member named "useNavigate" available for export

I'm attempting to include useNavigate for use as outlined in the top answer here: react button onClick redirect page import { useNavigate } from "react-router-dom"; However, I am encountering this error: export 'useNavigate' (impo ...

Error: The variable "redirectTo" has not been declared. This error can be found in the

I recently embarked on my journey to learn Angular2. I diligently followed the documentation and watched some helpful YouTube tutorials to guide me through the process. However, I've hit a roadblock while trying to configure the routes in my project. ...

What is the recommended approach for sending a null value to a mandatory property in a Vue.js component?

Setup: Vue.js (3.2) with Composition API, TypeScript, and Visual Studio Code File type.ts: export class GeographicCoordinate { latitude: number; longitude: number; altitude?: number; constructor(latitude: number, longitude: number, altitude?: num ...

Production environment experiencing issues with Custom Dashboard functionality for AdminJS

I have successfully integrated AdminJS into my Koa nodejs server and it works perfectly in my local environment. My objective is to override the Dashboard component, which I was able to do without any issues when not running in production mode. However, wh ...