Open In App

Angular PrimeNG Form TreeSelect Templates

Improve
Improve
Like Article
Like
Save
Share
Report

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 be seeing the Angular PrimeNG Form TreeSelect Component.

The TreeSelect Component allows the users to select items from hierarchical data. It accepts an array of TreeNodes as its options property to show the data. The Templates like the footer, header, and value are used to put some content on some pre-structured containers. The table provides templates to customize the content of various sections.

Syntax:

<p-treeSelect [(ngModel)]="..."
    selectionMode="..." 
    display="..."
    [options]="..." 
    placeholder="..."
    <ng-template let-group pTemplate="...">
        ...
    </ng-template>
</p-treeSelect>

Angular PrimeNG Form TreeSelect Templates:

  • value: It is the component’s value.
  • header: It is the component’s value. It is used to configure the template at the header.
  • footer: It is the component’s value.  It is used to configure the template at the footer.
  • empty: It is used to specify when there are no options available.

Creating Angular Application and Installing the Module:

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: Finally, Install PrimeNG in your given directory.

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

Project Structure: The project Structure will look like this after following the above steps:

 

  • Steps to run the application: Run the below command to see the output
ng serve --save 

Example 1: In the below code, we will make use of the above syntax to demonstrate the use of the Form TreeSelect Templates.

  • app.component.html:

HTML




<div style="text-align:center">
    <h2 style="color: green">
          GeeksforGeeks
      </h2>
    <h3>
          Angular PrimeNG Form TreeSelect Templates
      </h3>
 
    <p-treeSelect [(ngModel)]="selected"
                  selectionMode="single"
                  [options]="nodes"
                  placeholder="Select a Node">
        <ng-template let-group pTemplate="header">
              Header Content
        </ng-template>
    </p-treeSelect>
</div>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { TreeNode } from "primeng/api";
 
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
})
 
export class AppComponent {
    nodes: TreeNode[] = [];
    selected: any;
    ngOnInit() {
        this.nodes = [
            {
                "label": "Work",
                "icon": "pi pi-folder",
                "children": [
                    {
                        "label": "data.json",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "sales.docx",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "presentation.pptx",
                        "icon": "pi pi-file"
                    }
                ]
            },
            {
                "label": "Home",
                "icon": "pi pi-folder",
                "children": [
                    {
                        "label": "grocery.word",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "picture.jpeg",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "homeplan.png",
                        "icon": "pi pi-file"
                    }
                ]
            },
            {
                "label": "Multimedia",
                "icon": "pi pi-folder",
                "children": [
                    {
                        "label": "infinity-war.mp4",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "you.mp3",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "endgame.mp4",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "MI.mp4",
                        "icon": "pi pi-file"
                    }
                ]
            }
        ];
    }
}


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule }
    from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TreeSelectModule }
    from 'primeng/treeselect';
import {FormsModule} from '@angular/forms';
 
@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    TreeSelectModule
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
 
})
 
export class AppModule { }


Output:

 

Example 2: The below code example demonstrates the use of the Form TreeSelect Templates.

  • app.coponent.html:

HTML




<div style="text-align:center">
    <h2 style="color: green">
          GeeksforGeeks
      </h2>
    <h3>
          Angular PrimeNG Form TreeSelect Templates
      </h3>
 
    <p-treeSelect [(ngModel)]="selected"
                    selectionMode="single"
                    [options]="nodes"
                    placeholder="Select a Node">
      <ng-template let-group pTemplate="footer">
            Footer Content
      </ng-template>
    </p-treeSelect>
</div>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { TreeNode } from "primeng/api";
 
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
})
 
export class AppComponent {
    nodes: TreeNode[] = [];
    selected: any;
    ngOnInit() {
        this.nodes = [
            {
                "label": "Work",
                "icon": "pi pi-folder",
                "children": [
                    {
                        "label": "data.json",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "sales.docx",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "presentation.pptx",
                        "icon": "pi pi-file"
                    }
                ]
            },
            {
                "label": "Home",
                "icon": "pi pi-folder",
                "children": [
                    {
                        "label": "grocery.word",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "picture.jpeg",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "homeplan.png",
                        "icon": "pi pi-file"
                    }
                ]
            },
            {
                "label": "Multimedia",
                "icon": "pi pi-folder",
                "children": [
                    {
                        "label": "infinity-war.mp4",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "you.mp3",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "endgame.mp4",
                        "icon": "pi pi-file"
                    },
                    {
                        "label": "MI.mp4",
                        "icon": "pi pi-file"
                    }
                ]
            }
        ];
    }
}


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule }
    from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TreeSelectModule }
    from 'primeng/treeselect';
import {FormsModule} from '@angular/forms';
 
@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    TreeSelectModule
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
 
})
 
export class AppModule { }


Output:

 

Reference: https://primefaces.org/primeng/treeselect



Last Updated : 21 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads