Angular PrimeNG Form TreeSelect Templating
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 Templating 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. By default, the option’s label is utilized to render the display text of an item. To customize the content, we can simply define the value template which will retrieve the selected nodes as a parameter. We can also use the header, footer, and empty slots for further customization.
Syntax:
<p-treeSelect [(ngModel)]="...." selectionMode="...." [options]="...." placeholder="...."> <ng-template pTemplate="..."> ...... </ng-template> </p-treeSelect>
Angular PrimeNG Form TreeSelect Component Templates:
- value: It is the component’s value.
- header: It is the component’s value. The options are TreeNode options.
- footer: It is the component’s value. The options are TreeNode options.
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: This example demonstrates the use of the Form TreeSelect Templating in Angular PrimeNG.
- app.component.html:
HTML
< div style = "text-align:center" > < h2 style = "color: green" > GeeksforGeeks </ h2 > < h3 > Angular PrimeNG Form TreeSelect Templating </ h3 > < p-treeSelect [(ngModel)]="selectedValue2" selectionMode = "multiple" [options]="nodes" placeholder = "Select Items" > < ng-template pTemplate = "header" > Header Content </ ng-template > </ p-treeSelect > </ div > |
- app.component.ts:
HTML
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 { FormsModule } from '@angular/forms' ; import { TreeSelectModule } from 'primeng/treeselect' ; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, TreeSelectModule, FormsModule, ], declarations: [AppComponent], bootstrap: [AppComponent], }) export class AppModule {} |
Output:
.gif)
Example 2: This example demonstrates the use of the Form TreeSelect Templating in Angular PrimeNG. Here, we have implemented the footer template.
- app.component.html:
HTML
< div style = "text-align:center" > < h2 style = "color: green" > GeeksforGeeks </ h2 > < h3 > Angular PrimeNG Form TreeSelect Templating </ h3 > < p-treeSelect [(ngModel)]="selectedValue2" selectionMode = "multiple" [options]="nodes" placeholder = "Select Items" > < ng-template pTemplate = "footer" > Footer Content </ ng-template > </ p-treeSelect > </ div > |
- app.componen.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 { FormsModule } from '@angular/forms' ; import { TreeSelectModule } from 'primeng/treeselect' ; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, TreeSelectModule, FormsModule, ], declarations: [AppComponent], bootstrap: [AppComponent], }) export class AppModule {} |
Output:
.gif)
Reference: https://primefaces.org/primeng/treeselect
Please Login to comment...