DEV Community

Cover image for Improving Angular's Performance by Implementing Virtual Scrolling on MatTable.
DHIRAJ SHETTY
DHIRAJ SHETTY

Posted on

Improving Angular's Performance by Implementing Virtual Scrolling on MatTable.

Virtual Scroll is one of the most commonly employed concepts for enhancing Angular performance. Let’s explore how we can harness its power with a MatTable.

Installation:
Make Sure you are running your application on Angular 9.1.13 or Above.

Npm:

npm install mat-table-virtual-scroll
Enter fullscreen mode Exit fullscreen mode

Setup:
Include TableModule in your application’s app.module.ts.

import { TableModule } from "mat-table-virtual-scroll";
@NgModule({
  imports: [MaterialModule, CommonModule, RouterModule, TableModule],
  declarations: components,
  exports: [TableModule, MaterialModule],
})
export class ComponentsModule {}
Enter fullscreen mode Exit fullscreen mode

Create a dataset of over 10,000 records in your app.component.ts.

import { Component } from '@angular/core';
import { ColumnDef } from 'mat-table-virtual-scroll';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  title = 'mat-table-virtual-scroll';
  name = 'Angular';
  rows = Array(10000)
    .fill(0)
    .map((x, i) => {
      return { name: 'name' + i, id: i, age: 27 };
    });
  columns: ColumnDef[] = [
    { field: 'name', title: 'Name' },
    { field: 'id', title: 'Id' },
    { field: 'age', title: 'Age' },
  ];
}
Enter fullscreen mode Exit fullscreen mode
  • The ‘rows’ will be an array of JSON objects
  • The ‘columns’ will be an array of JSON objects.
  • The ‘field’ will be the key extracted from the ‘rows’ JSON array.
  • The ‘title’ will serve as the header for the columns.

Integrate ‘mat-virtual-table’ into your app.component.html.

<mat-virtual-table [rows]="rows" [columnsDef]="columns"></mat-virtual-table>
Enter fullscreen mode Exit fullscreen mode

Mat Table Virtual Scroll

Enhancing column customization can be achieved by using ng-templates.

<mat-virtual-table [rows]="rows" [columnsDef]="columns">
  <ng-template pCellDef column="name" let-row="row">
    <b>{{row.name}}</b>
  </ng-template>
</mat-virtual-table>
Enter fullscreen mode Exit fullscreen mode

You can hide rows based on specific conditions.

<mat-virtual-table [rows]="rows" [columnsDef]="columns" [hiddenData]="name" [hiddenValue]="name0"></mat-virtual-table>
Enter fullscreen mode Exit fullscreen mode

StackBlitz Code

Thanks for reading. 🚀

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)

Warp.dev image

The best coding agent. Backed by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp