Open In App

Angular PrimeNG Tree Loading Status

Improve
Improve
Like Article
Like
Save
Share
Report

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 Tree Loading Status.

The tree component is used to hierarchical data to the user. The loading property of the tree is used to show the spinner icon to the user indicating that the data is loading in the background. Additionally, the loadingIcon property can be used to customize the icon used to show the loading status of the tree.

Syntax:

<p-tree [value]="..." [loading]= "true || false"></p-tree>

 

Creating Angular application and Installing the Modules:

Step 1: Create an Angular application using the following command.

ng new myapp

Step 2: After creating your project folder i.e. myapp, move to it using the following command.

cd myapp

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: After completing the above steps the project structure will look like the following.

Project Structure

Example 1: This example illustrates the use of the loading property of the tree component to set the loading status of the tree to true.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h4>Angular PrimeNG Tree Loading Status</h4>
  
    <p-tree 
        [value]="nodes" 
        [loading]="isLoading">
    </p-tree>
  
    <button 
        pButton 
        label="Load" 
        (click)="btnClick()" 
        style="
            position: absolute; 
            bottom: 50px; 
            right: 0
        ">
    </button>
</div>


app.component.ts




import { Component } from '@angular/core';
import { TreeNode } from 'primeng/api';
import { NodeService } from './nodeservice';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    nodes: TreeNode[] = [];
    isLoading = false;
  
    constructor(private nodeService: NodeService) { }
  
    ngOnInit() {
        this.isLoading = true;
          
        setTimeout(() => {
            this.nodes = [
                {
                    label: 'Files',
                    icon: 'pi pi-folder',
  
                    children: [
                        {
                            label: 'Office',
                            icon: 'pi pi-folder',
  
                            children: [
                                {
                                    label: 'MS Excel',
                                    icon: 'pi pi-folder',
                                },
                                {
                                    label: 'MS Word',
                                    icon: 'pi pi-folder',
                                },
                            ],
                        },
                        {
                            label: 'Home',
                            icon: 'pi pi-folder',
  
                            children: [
                                {
                                    label: 'Movies',
                                    icon: 'pi pi-folder',
                                },
                            ],
                        },
                    ],
                },
                {
                    label: 'Docs',
                    icon: 'pi pi-folder',
  
                    children: [
                        {
                            label: 'Academic',
                            icon: 'pi pi-folder',
                        },
                        {
                            label: 'Loans',
                            icon: 'pi pi-folder',
                        },
                        {
                            label: 'Investments',
                            icon: 'pi pi-folder',
                        },
                    ],
                },
            ];
  
            this.isLoading = false;
        }, 2000);
    }
  
    btnClick()
    {
        this.nodes = [];
        this.ngOnInit();
    }
}


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 { AppComponent } from './app.component';
import { NodeService } from './nodeservice';
import { TreeModule } from 'primeng/tree';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeModule,
        ButtonModule,
        HttpClientModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [NodeService],
})
export class AppModule { }


Output:

 

Example 2: In this example, we changed the loading icon from a spinner icon to a star icon using the loadingIcon property.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h4>Angular PrimeNG Tree Loading Status</h4>
  
    <p-tree 
        [value]="nodes"
        loadingIcon="pi pi-star pi-spin" 
        [loading]="isLoading">
    </p-tree>
  
    <button 
        pButton 
        label="Load" 
        (click)="btnClick()" 
        style="
            position: absolute; 
            bottom: 50px; 
            right: 0
        ">
    </button>
</div>


app.component.ts




import { Component } from '@angular/core';
import { TreeNode } from 'primeng/api';
import { NodeService } from './nodeservice';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    nodes: TreeNode[] = [];
    isLoading = false;
  
    constructor(private nodeService: NodeService) { }
  
    ngOnInit() {
        this.isLoading = true;
          
        setTimeout(() => {
            this.nodes = [
                {
                    label: 'Files',
                    icon: 'pi pi-folder',
  
                    children: [
                        {
                            label: 'Office',
                            icon: 'pi pi-folder',
  
                            children: [
                                {
                                    label: 'MS Excel',
                                    icon: 'pi pi-folder',
                                },
                                {
                                    label: 'MS Word',
                                    icon: 'pi pi-folder',
                                },
                            ],
                        },
                        {
                            label: 'Home',
                            icon: 'pi pi-folder',
  
                            children: [
                                {
                                    label: 'Movies',
                                    icon: 'pi pi-folder',
                                },
                            ],
                        },
                    ],
                },
                {
                    label: 'Docs',
                    icon: 'pi pi-folder',
  
                    children: [
                        {
                            label: 'Academic',
                            icon: 'pi pi-folder',
                        },
                        {
                            label: 'Loans',
                            icon: 'pi pi-folder',
                        },
                        {
                            label: 'Investments',
                            icon: 'pi pi-folder',
                        },
                    ],
                },
            ];
  
            this.isLoading = false;
        }, 2000);
    }
  
    btnClick()
    {
        this.nodes = [];
        this.ngOnInit();
    }
}


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 { AppComponent } from './app.component';
import { NodeService } from './nodeservice';
import { TreeModule } from 'primeng/tree';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeModule,
        ButtonModule,
        HttpClientModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [NodeService],
})
export class AppModule { }


Output:

 

Reference: https://www.primefaces.org/primeng/tree



Last Updated : 26 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads