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

Converting JavaScript object data to x-www-form-urlencoded: A step-by-step guide

I am trying to convert a JavaScript object into x-www-form-urlencoded. Is there a way to achieve this using Angular 2? export class Compentency { competencies : number[]; } postData() { let array = [1, 2, 3]; this.comp.competencies ...

Create a new visual masterpiece using Canvas by repurposing an

I am currently working on the following code snippet; export default async function draw(elRef : RefObject<HTMLCanvasElement>, tileData : TileProps) { const canvas = elRef.current!; const ctx = canvas.getContext('2d')!; ctx.clearRect( ...

The React-Typescript error message is stating that the module "react-router-dom" does not have the exported member "RouteComponentProps"

I encountered an issue with my project involving a login page and the usage of "RouteComponentProps". Unfortunately, I received the following error: Module '"react-router-dom"' has no exported member 'RouteComponentProps'. Upon attempt ...

What is the best way to confirm that a certain element is not present on the page using playwright?

My current challenge involves testing a website that features a logo, and I need to ensure that the logo does not display on specific pages. I have been exploring the Playwright assertions documentation for guidance on how to assert that an element does N ...

How can I use Angular2 to ensure that a child component automatically updates when there is a change in the parent's property?

After reviewing the components provided below, it appears that the parent's property updates correctly, but unfortunately, the changes are not reflected in the child component. What steps should I take to ensure that the child component accurately ref ...

retrieve object from s3 using angular and open it as a pdf

I'm attempting to access a file from an s3 bucket using Angular and display it as a PDF. I have a node service set up to retrieve the object, which I then call from Angular. However, when I try to open the PDF in Angular, it appears as a blank (white) ...

Omit an enum item from selection when referencing the key within the Enum

Within my enum, I have defined multiple keys: export enum MyTypeEnum { one = 'one', two = 'two', three = 'three', four = 'four' } To ensure certain types must contain these keys, I use the following ...

When I click on the md-select element, a superfluous "overflow-y: scroll;" gets added to the <body> of my webpage

Typically, I have the following styles on my body: element.style { -webkit-app-region: drag; } However, when I interact with a md-select element (you can observe this behavior on the provided link), additional styles are applied. element.style { -w ...

Neither Output nor EventEmitter are transmitting data

I am struggling to pass data from my child component to my parent component using the Output() and EventEmitter methods. Despite the fact that the emitter function in the child component is being called, it seems like no data is actually being sent through ...

Expanding properties in a React component based on certain conditions using TypeScript

I am attempting to dynamically expand my component props based on whether a specific prop is included. The goal is to add attributes from an anchor if the href prop is provided, and include attributes from a button if it is not. Is this achievable? Chec ...

Is it possible to import the identical file twice consecutively using html and typescript?

I encountered an issue with an input element in my HTML file. Here's what it looks like: <input type="file" (change)="receiveFile($event)" id="inputFileButton" hidden /> This input element is designed for users to import files. Wh ...

How can I attach a click event listener to an Angular 2 ui-router <section ui-view> element?

In my Angular 2 application, I have a list called 'views' that contains paths to multiple images. Using ng-repeat, I cycle through these images and display them one by one in a ui-view element. I want to add an event listener so that when an imag ...

Organize library files into a build directory using yarn workspaces and typescript

When setting up my project, I decided to create a yarn workspace along with typescript. Within this workspace, I have three folders each with their own package.json /api /client /lib The main goal is to facilitate code sharing between the lib folder and b ...

How to dynamically inject HTML content from a child component into a different component using Angular 5

Is it possible to customize the content of a reusable header section based on the current route data in Angular? The header currently displays a title and description pulled from the route data property. My concern is how to dynamically inject different H ...

"Tips for removing all child elements from a parent element using Angular's Renderer2 in versions 5 and above

Manipulating the DOM programmatically is recommended using Angular's Renderer2. Within my directive, I extract some text from el.nativeElement.innerText, modify it, and aim to append it to my element: const text = renderer.createText(`${el.innerTex ...

During the present module, retrieve the runtime list of all modules that are directly imported (Javascript/Typescript)

Imagine you have a set of modules imported in the current module: import {A1, A2, A3} from "./ModuleA"; import {B1, B2, B3} from "./ModuleB"; import {C1, C2, C3} from "./ModuleC"; function retrieveListOfImportedModules() { // ...

Personalized ornamentation using TypeScript

Is there a way to access the variables of the class when using a decorator? @ExampleDecorator() export class UserController { private userData: string = "example"; } export const ExampleDecorator = (config: IConfigSettings) => (target: Object) =&g ...

Unable to pass parameters through the URL in Angular's POST request

Hey there! I'm currently facing an issue with passing a parameter in the POST method URL within an Angular service to construct a URL for fetching data from an API. However, when I try calling it in the component file, I keep getting an error response ...

How can I define Record values in Typescript based on their specific keys?

I am working on creating a custom data structure that allows me to store values with string keys within the union string | number | boolean: type FilterKey = string; type FilterValue = string | number | boolean; type Filters<K extends FilterKey, T exten ...

How can we define a function using a generic type in this scenario using Typescript?

Here's a challenge that I'm facing. I have this specific type definition: type FuncType<T> = (value: T) => T I want to create a function using this type that follows this structure: const myFunc: FuncType<T> = (value) => valu ...