Angular PrimeNG Table Lazy Loading
Last Updated :
14 Sep, 2022
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Table Lazy Loading in Angular PrimeNG.
Angular PrimeNG Table Lazy Loading is used to lazy load the data of the TreeTable component. It loads the data when required and makes the TreeTable more interactive and user-friendly.
Syntax:
// In app.component.html
<p-table
[columns]="cols"
[value]="tableData"
[lazy]="true"
(onLazyLoad)="loadNodes($event)"
[loading]="loading">
</p-table>
// In app.component.ts
loadNodes(event) {
// lazy function
}
Creating Angular application & Module Installation:
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Step 3: Install PrimeNG in your given directory.
npm install primeng --save
npm install primeicons --save
Project Structure: It will look like the following:
Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG Table Lazy Loading.
app.component.html
<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Table Lazy Loading</h4>
<p-table
[value]="tableData"
[lazy]="true"
(onLazyLoad)="loadNodes($event)"
[loading]="loading">
<ng-template pTemplate="header">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-people>
<tr>
<td>{{ people.firstname }}</td>
<td>{{ people.lastname }}</td>
<td>{{ people.age }}</td>
</tr>
</ng-template>
</p-table>
app.component.ts
import { Component } from '@angular/core';
import { NodeService } from './nodeservice';
interface People {
firstname?: string;
lastname?: string;
age?: string;
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
tableData: People[] = [];
cols: any[] = [];
loading: boolean = false;
constructor(private nodeService: NodeService) { }
ngOnInit() {
this.cols = [
{ field: 'firstname', header: 'First Name' },
{ field: 'lastname', header: 'Last Name' },
{ field: 'age', header: 'Age' },
];
this.loading = true;
}
loadNodes(event: any) {
this.loading = true;
setTimeout(() => {
this.loading = false;
this.tableData = [
{
firstname: 'David',
lastname: 'ace',
age: '40',
},
{
firstname: 'AJne',
lastname: 'west',
age: '40',
},
{
firstname: 'Mak',
lastname: 'Lame',
age: '40',
},
{
firstname: 'Peter',
lastname: 'raw',
age: '40',
},
{
firstname: 'Kane',
lastname: 'James',
age: '40',
},
];
}, 1000);
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { NodeService } from './nodeservice';
import { TreeTableModule } from 'primeng/treetable';
import { ButtonModule } from 'primeng/button';
import { DialogModule } from 'primeng/dialog';
import { MultiSelectModule } from 'primeng/multiselect';
import { InputTextModule } from 'primeng/inputtext';
import { ToastModule } from 'primeng/toast';
import { ContextMenuModule } from 'primeng/contextmenu';
import { TableModule } from 'primeng/table';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TreeTableModule,
ToastModule,
TableModule,
DialogModule,
ButtonModule,
MultiSelectModule,
InputTextModule,
ContextMenuModule,
HttpClientModule,
FormsModule,
RouterModule.forRoot([{ path: '', component: AppComponent }]),
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [NodeService],
})
export class AppModule { }
Output:
Example 2: Below is another example demonstrating the use of the Angular PrimeNG Table Lazy Loading. In this example, we lazy-loaded the data for 5 seconds.
app.component.html
<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Table Lazy Loading</h4>
<p-table
[value]="tableData"
[lazy]="true"
(onLazyLoad)="loadNodes($event)"
[loading]="loading">
<ng-template pTemplate="header">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-people>
<tr>
<td>{{ people.firstname }}</td>
<td>{{ people.lastname }}</td>
<td>{{ people.age }}</td>
</tr>
</ng-template>
</p-table>
app.component.ts
import { Component, OnInit } from '@angular/core';
import { NodeService } from './nodeservice';
interface People {
firstname?: string;
lastname?: string;
age?: string;
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
tableData: People[] = [];
cols: any[] = [];
loading: boolean = false;
constructor(private nodeService: NodeService) { }
ngOnInit() {
this.cols = [
{ field: 'firstname', header: 'First Name' },
{ field: 'lastname', header: 'Last Name' },
{ field: 'age', header: 'Age' },
];
this.loading = true;
}
loadNodes(event: any) {
this.loading = true;
setTimeout(() => {
this.loading = false;
this.tableData = [
{
firstname: 'David',
lastname: 'ace',
age: '40',
},
{
firstname: 'AJne',
lastname: 'west',
age: '40',
},
{
firstname: 'Mak',
lastname: 'Lame',
age: '40',
},
{
firstname: 'Peter',
lastname: 'raw',
age: '40',
},
{
firstname: 'Kane',
lastname: 'James',
age: '40',
},
];
}, 5000);
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { NodeService } from './nodeservice';
import { TreeTableModule } from 'primeng/treetable';
import { ButtonModule } from 'primeng/button';
import { DialogModule } from 'primeng/dialog';
import { MultiSelectModule } from 'primeng/multiselect';
import { InputTextModule } from 'primeng/inputtext';
import { ToastModule } from 'primeng/toast';
import { ContextMenuModule } from 'primeng/contextmenu';
import { TableModule } from 'primeng/table';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TreeTableModule,
ToastModule,
TableModule,
DialogModule,
ButtonModule,
MultiSelectModule,
InputTextModule,
ContextMenuModule,
HttpClientModule,
FormsModule,
RouterModule.forRoot([{ path: '', component: AppComponent }]),
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [NodeService],
})
export class AppModule { }
Output:
Reference: https://primefaces.org/primeng/table/lazy
Similar Reads
Angular PrimeNG TreeTable Lazy Loading
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use TreeTable Lazy Loading in Angular PrimeNG. Angular PrimeNG Tree
7 min read
Angular PrimeNG TabView Lazy Loading
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use TabView Lazy Loading in Angular PrimeNG. Angular PrimeNG fac
3 min read
Angular PrimeNG Tree Lazy Loading
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Tree Lazy Loading in Angular PrimeNG. Angular PrimeNG Tree Lazy
4 min read
Angular PrimeNG Table Lazy
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Table Lazy in Angular PrimeNG. Angular PrimeNG Table Lazy is us
4 min read
Angular PrimeNG Table Layout
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more.
5 min read
Angular PrimeNG Table Loading Status
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see Angular PrimeNG Table Loading Status. The Table component shows some da
4 min read
Angular PrimeNG Table Cell Editing
Angular PrimeNG is an open-source library that consists of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see Angular PrimeNG Table Cell Editing. The Table Component is used to show som
5 min read
Angular PrimeNG TreeTable Lazy
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use TreeTable Lazy Loading in Angular PrimeNG. Angular PrimeNG T
7 min read
Angular PrimeNG Table VirtualScroller Lazy Loading
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the Angular PrimeNG VirtualScroller Lazy Loading. VirtualSc
6 min read
Angular PrimeNG Table Page
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use Table Page in Angular PrimeNG. Angular PrimeNG Table Page en
5 min read