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 TreeTable 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-treeTable [columns]="cols" [value]="tableData" [lazy]="true" (onLazyLoad)="loadNodes($event)" [loading]="loading"> ... </p-treeTable> // 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 TreeTable Lazy Loading.
< h2 style = "color: green" >GeeksforGeeks</ h2 >
< h4 >Angular PrimeNG TreeTable Lazy Loading</ h4 >
< p-treeTable [columns]="cols"
[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-rowNode
let-rowData = "rowData" >
< tr >
< td >
< p-treeTableToggler [rowNode]="rowNode">
</ p-treeTableToggler >
{{ rowData.firstname }}
</ td >
< td >{{ rowData.lastname }}</ td >
< td >{{ rowData.age }}</ td >
</ tr >
</ ng-template >
</ p-treeTable >
|
import { Component, OnInit } from '@angular/core' ;
import { NodeService } from './nodeservice' ;
import { TreeNode } from 'primeng/api' ;
@Component({ selector: 'app-root' ,
templateUrl: './app.component.html'
}) export class AppComponent { tableData: TreeNode[] = [];
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 .tableData = [
{
data: {
firstname: 'David' ,
lastname: 'ace' ,
age: '40' ,
},
children: [
{
data: {
firstname: 'Nathan' ,
lastname: 'ace' ,
age: '16' ,
},
children: [
{
data: {
firstname: 'Abe' ,
lastname: 'ace' ,
age: '12' ,
},
},
{
data: {
firstname: 'Ksi' ,
lastname: 'ace' ,
age: '12' ,
},
},
],
},
{
data: {
firstname: 'Shane' ,
lastname: 'ace' ,
age: '14' ,
},
},
],
},
{
data: {
firstname: 'Warner' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
lastname: 'ace' ,
firstname: 'Michelle' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'Max' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'Willy' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'Miley' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'Sam' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'James' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
lastname: 'ace' ,
firstname: 'Charlie' ,
age: '24' ,
},
},
],
},
];
this .loading = false ;
}, 1000);
}
} |
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 { AppComponent } from './app.component' ;
import { NodeService } from './nodeservice' ;
import { TreeTableModule } from 'primeng/treetable' ;
import { ButtonModule } from 'primeng/button' ;
import { InputTextModule } from 'primeng/inputtext' ;
@NgModule({ imports: [
BrowserModule,
BrowserAnimationsModule,
TreeTableModule,
ButtonModule,
InputTextModule,
HttpClientModule,
FormsModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [NodeService],
}) export class AppModule { } |
Output:
Example 2: Below is another example demonstrating the use of the Angular PrimeNG TreeTable Lazy Loading. In this example, we are lazy loading the data for 5 seconds.
app.component.ts
< h2 style = "color: green" >GeeksforGeeks</ h2 >
< h4 >Angular PrimeNG TreeTable Lazy Loading</ h4 >
< p-treeTable [columns]="cols"
[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-rowNode
let-rowData = "rowData" >
< tr >
< td >
< p-treeTableToggler [rowNode]="rowNode">
</ p-treeTableToggler >
{{ rowData.firstname }}
</ td >
< td >{{ rowData.lastname }}</ td >
< td >{{ rowData.age }}</ td >
</ tr >
</ ng-template >
</ p-treeTable >
|
import { Component, OnInit } from '@angular/core' ;
import { NodeService } from './nodeservice' ;
import { TreeNode } from 'primeng/api' ;
@Component({ selector: 'app-root' ,
templateUrl: './app.component.html' ,
}) export class AppComponent { tableData: TreeNode[] = [];
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 .tableData = [
{
data: {
firstname: 'David' ,
lastname: 'ace' ,
age: '40' ,
},
children: [
{
data: {
firstname: 'Nathan' ,
lastname: 'ace' ,
age: '16' ,
},
children: [
{
data: {
firstname: 'Abe' ,
lastname: 'ace' ,
age: '12' ,
},
},
{
data: {
firstname: 'Ksi' ,
lastname: 'ace' ,
age: '12' ,
},
},
],
},
{
data: {
firstname: 'Shane' ,
lastname: 'ace' ,
age: '14' ,
},
},
],
},
{
data: {
firstname: 'Warner' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
lastname: 'ace' ,
firstname: 'Michelle' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'Max' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'Willy' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'Miley' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'Sam' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
firstname: 'Charlie' ,
lastname: 'ace' ,
age: '24' ,
},
},
],
},
{
data: {
firstname: 'James' ,
lastname: 'ace' ,
age: '55' ,
},
children: [
{
data: {
firstname: 'Michelle' ,
lastname: 'ace' ,
age: '20' ,
},
},
{
data: {
lastname: 'ace' ,
firstname: 'Charlie' ,
age: '24' ,
},
},
],
},
];
this .loading = false ;
}, 5000);
}
} |
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 { AppComponent } from './app.component' ;
import { NodeService } from './nodeservice' ;
import { TreeTableModule } from 'primeng/treetable' ;
import { ButtonModule } from 'primeng/button' ;
import { InputTextModule } from 'primeng/inputtext' ;
@NgModule({ imports: [
BrowserModule,
BrowserAnimationsModule,
TreeTableModule,
ButtonModule,
InputTextModule,
HttpClientModule,
FormsModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [NodeService],
}) export class AppModule { } |
Output:
Reference: https://primefaces.org/primeng/treetable/lazy