Virtual Scrolling- Angular

Virtual Scrolling- Angular

Sometimes we as a developer have to show thousands of elements in a single table. When we add these items to one DOM (Document Object Model), it will impact the performance. We can handle this in many ways like pagination, infinite scroll, and virtual scroll.

Pagination: It helps us to list and show items as chunks it works as expected but we have to click back and forward between pages.

Infinite Scroll: At first, it loads a few items and when we scroll, it appends more items. But when more items keep added, the app is going to slow down.?

The virtual scroll is one of the options which helps us to display thousands of items without slowing the app down.

Virtual Scrolling is one of the famous features of Angular 7. This feature included to CDK means 'Component Development Kit'. The virtual scroll shows up the dom elements which are ready to see to the users. By scrolling down it will display the next list. This feature allows only one list at a time which is visible on the screen, this can help in performance and it will be faster than rendering the whole list in one go.

Installation:

Make sure we are using Angular 7 or a higher version to apply virtual scrolling using CDK. Using the following command we can add Angular CDK to our application. After reaching the path of our application executes this command:

npm install @angular/cdk
?
ng add @angular/cdk
        

We have done with installing the dependency of the virtual scrolling module and now move forward to the next step. We are going to create our app for a better understanding of virtual scrolling. We can also implement API data with virtual scroll if there are thousands of items coming from API and we require it to show on-grid or table.

Setup of software:

?First of all, we're going to upload ScrollingModule in our app.module.ts record equal like below:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';?
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ScrollingModule } from '@angular/cdk/scrolling';??

@NgModule({

? declarations: [
??? AppComponent
? ],

? imports: [
??? BrowserModule,
??? AppRoutingModule,
??? ScrollingModule

? ],

? providers: [],
? bootstrap: [AppComponent]

})

export class AppModule { }        

After that, we have to generate a list of items in our app.component.ts

import { Component } from '@angular/core'
?

@Component({
? selector: 'app-root',
? templateUrl: './app.component.html',
? styleUrls: ['./app.component.css']
})

export class AppComponent {
? numbers: number[] = [];?

? constructor() {
??? for (let index = 0; index < 10000; index++) {
????? this.numbers.push(index);
??? }
? }

};
        

Next, we have to render this list in our HTML file like below.?

<h1 class="center">Virtual Scroll using Angular 7</h1>
<ul class="list">
? <cdk-virtual-scroll-viewport? style="height: 500px" itemSize="90" >
??? <ng-container *cdkVirtualFor="let n of numbers">
????? <li> {{n}} </li>
??? </ng-container>
? </cdk-virtual-scroll-viewport>
</ul>>        

We can also add a custom style to make app layout more attractive. Below is the output for the same code and we can observe on scrolling down we get the item in chunks and our app will not slow down because of the logic of virtual scrolling.?

No alt text provided for this image

要查看或添加评论,请登录

Sandip Poojara的更多文章

  • A new way to validate Angular Forms

    A new way to validate Angular Forms

    If you're working on more data-oriented advanced reactive forms, you'll find that not just inputs, but some of the…

  • Razor Pages in ASP.NET Core

    Razor Pages in ASP.NET Core

    In this blog, we are going to discuss Razor pages with Asp.net Core.

    1 条评论
  • Best Practices when using Angular CLI

    Best Practices when using Angular CLI

    Angular is one of the best platforms to create Single Page Applications (SPA). We will go through several practices…

    1 条评论
  • Angular Syncfusion grid

    Angular Syncfusion grid

    Angular is one of the exceptional frameworks of JavaScript. We can use Essential JS 2 grid with angular, which is…

  • MEAN Stack CRUD- Angular 13

    MEAN Stack CRUD- Angular 13

    In this blog, we will look into how to perform Angular 13 crud operation using MEAN stack technology. We will use…

  • Debugging Node.js with Google Chrome

    Debugging Node.js with Google Chrome

    In software development, debugging is used when a developer locates a code error in a particular program and is able to…

  • Interaction of NodeJS Services with JSON content using AJAX

    Interaction of NodeJS Services with JSON content using AJAX

    Node.js is an "open-source server-side runtime platform.

  • Lazy Loading Modules in Angular 8

    Lazy Loading Modules in Angular 8

    Lazy loading is a very helpful concept in Angular. It loads NgModuls only when we navigate to rout.

    1 条评论

社区洞察

其他会员也浏览了