Angular PrimeNG Tree Lazy Loading
Last Updated :
25 Oct, 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 Tree Lazy Loading in Angular PrimeNG.
Angular PrimeNG Tree Lazy Loading is used to lazy load the data of the Tree component. It loads the data when required and makes the Tree component more interactive and user-friendly.
Syntax:
// In app.component.html
<p-tree [value]="..." [loading]="loading">
</p-tree>
// In app.component.ts
this.loading = true;
setTimeout(() => {
// lazy function
}, 1000);
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 Tree Lazy Loading.
HTML
< h2 style = "color: green;" >
GeeksforGeeks
</ h2 >
< h4 >Angular PrimeNG Tree Lazy Loading</ h4 >
< p-tree [value]="files1"
scrollHeight = "300px"
[loading]="loading">
</ p-tree >
|
Javascript
import { Component, OnInit } from '@angular/core' ;
import { TreeNode } from 'primeng/api' ;
@Component({
selector: 'app-root' ,
templateUrl: './app.component.html' ,
})
export class AppComponent {
files1: TreeNode[] = [];
loading: boolean = false ;
constructor() { }
ngOnInit() {
this .loading = true ;
setTimeout(() => {
this .loading = false ;
this .files1 = [
{ label: 'Z' , icon: 'pi pi-folder' },
{ label: 'A' , icon: 'pi pi-folder' },
{ label: 'D' , icon: 'pi pi-folder' },
{ label: 'C' , icon: 'pi pi-folder' },
{ label: 'W' , icon: 'pi pi-folder' },
{ label: 'E' , icon: 'pi pi-folder' },
{ label: 'R' , icon: 'pi pi-folder' },
{ label: 'G' , icon: 'pi pi-folder' },
{ label: 'K' , icon: 'pi pi-folder' },
{
label: 'A' ,
icon: 'pi pi-folder' ,
children: [
{
label: 'B' ,
icon: 'pi pi-folder' ,
children: [
{
label: 'C' ,
icon: 'pi pi-folder' ,
},
{
label: 'D' ,
icon: 'pi pi-folder' ,
},
],
},
{
label: 'E' ,
icon: 'pi pi-folder' ,
children: [
{
label: 'F' ,
icon: 'pi pi-folder' ,
},
],
},
],
},
{
label: 'G' ,
icon: 'pi pi-folder' ,
children: [
{
label: 'H' ,
icon: 'pi pi-folder' ,
},
{
label: 'I' ,
icon: 'pi pi-folder' ,
},
{
label: 'J' ,
icon: 'pi pi-folder' ,
},
],
},
];
}, 1000);
}
}
|
Javascript
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 { TreeModule } from 'primeng/tree' ;
import { ButtonModule } from 'primeng/button' ;
import { InputTextModule } from 'primeng/inputtext' ;
import { VirtualScrollerModule } from 'primeng/virtualscroller' ;
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TreeModule,
ButtonModule,
InputTextModule,
HttpClientModule,
FormsModule,
VirtualScrollerModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [],
})
export class AppModule { }
|
Output:
Example 2: Below is another example demonstrating the use of the Angular PrimeNG Tree Lazy Loading. In this example, we are using lazy loading with an emptymessage.
HTML
< h2 style = "color: green;" >
GeeksforGeeks
</ h2 >
< h4 >Angular PrimeNG Tree Lazy Loading</ h4 >
< p-tree [value]="files1"
scrollHeight = "300px"
[loading]="loading">
< ng-template pTemplate = "empty" >
No records found
</ ng-template >
</ p-tree >
|
Javascript
import { Component, OnInit } from '@angular/core' ;
import { TreeNode } from 'primeng/api' ;
@Component({
selector: 'app-root' ,
templateUrl: './app.component.html' ,
})
export class AppComponent {
files1: TreeNode[] = [];
loading: boolean = false ;
constructor() { }
ngOnInit() {
this .loading = true ;
this .files1 = [];
setTimeout(() => {
this .loading = false ;
this .files1 = [];
}, 2000);
}
}
|
Javascript
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 { TreeModule } from 'primeng/tree' ;
import { ButtonModule } from 'primeng/button' ;
import { InputTextModule } from 'primeng/inputtext' ;
import { VirtualScrollerModule } from 'primeng/virtualscroller' ;
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TreeModule,
ButtonModule,
InputTextModule,
HttpClientModule,
FormsModule,
VirtualScrollerModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [],
})
export class AppModule { }
|
Output:
Reference: https://primefaces.org/primeng/tree/
Share your thoughts in the comments
Please Login to comment...