Open In App

Angular PrimeNG Form CascadeSelect Templates Component

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 Angular PrimeNG Form CascadeSelect Templates Component.

The CascadeSelect Component is used to display a nested structure of options. It is like a Dropdown component where the items have their nested items which the user can select.



Angular PrimeNG Form CascadeSelect Templates Component: The templates are used to put some content on some pre-structured containers. CascadeSelect templates are discussed below:

Syntax:



 <p-cascadeSelect 
      [placeholder]="..." 
      [options]="..." 
       [(ngModel)]="..." 
     [options]="..." 
     optionLabel="..." 
     optionGroupLabel="..."
     [optionGroupChildren]="..." >
    <ng-template pTemplate="*"  >
     .......
    </ng-template>
</p-cascadeSelect>

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:

 

Step to run the application: Run the below command to see the output

ng serve --save

Example 1: In this example, we will learn about the value template.




<h1 style="color:green">
    GeeksforGeeks
</h1>
<h2>
    Angular PrimeNG Form CascadeSelect Templates Component
</h2>
<p-cascadeSelect
    [placeholder]="item"
    [options]="countries"
    [(ngModel)]="selectedCity2"
    [options]="countries"
    optionLabel="cname"
    optionGroupLabel="name"
    [optionGroupChildren]="['states', 'cities']" >
    <ng-template pTemplate="value"  >
         Select Something using Value Template
    </ng-template>
</p-cascadeSelect>




import { Component } from "@angular/core";
 
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
 
export class AppComponent {
    countries: any[] = [];
    item:string="GFG"
    selectedCity1: any;
 
    selectedCity2: any;
 
    ngOnInit() {
        this.countries = [
            {
                name: "India",
                code: "AU",
                states: [
                    {
                        name: "Mumbai",
                        cities: [
                            {
                                cname: "Nallasopara",
                                code: "NSP"
                            },
                            {
                                cname: "Mira Road",
                                code: "MR"
                            },
                            {
                                cname: "Dahisar",
                                code: "DHI"
                            }
                        ]
                    },
                    {
                        name: "UP",
                        cities: [
                            {
                                cname: "Varanasi",
                                code: "VR"
                            },
                            {
                                cname: "Balia",
                                code: "BA"
                            }
                        ]
                    }
                ]
            },
        ];
    }
}




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

Output:

 

Example 2: In this example, we will learn about option templates.




<h1 style="color:green">
    GeeksforGeeks
</h1>
<h2>
    Angular PrimeNG Form CascadeSelect Templates Component
</h2>
 
<p-cascadeSelect
    [(ngModel)]="selectedCity2"
    [options]="countries"
    optionLabel="cname"
    optionGroupLabel="name"
    [optionGroupChildren]="['states', 'cities']"
    [style]="{ minWidth: '14rem' }"
    placeholder="Select a City">
 
    <ng-template pTemplate="option" let-option>
        <div class="country-item">
            <i
                class="pi pi-compass p-mr-2"
                *ngIf="option.cities">
            </i>
            <i
                class="pi pi-map-marker p-mr-2"
                *ngIf="option.cname">
            </i>
            <span>
                {{ option.cname || option.name }}
            </span>
        </div>
    </ng-template>
</p-cascadeSelect>




import { Component } from "@angular/core";
 
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
 
export class AppComponent {
    countries: any[] = [];
    item:string="GFG"
    selectedCity1: any;
 
    selectedCity2: any;
 
    ngOnInit() {
        this.countries = [
            {
                name: "India",
                code: "AU",
                states: [
                    {
                        name: "Mumbai",
                        cities: [
                            {
                                cname: "Nallasopara",
                                code: "NSP"
                            },
                            {
                                cname: "Mira Road",
                                code: "MR"
                            },
                            {
                                cname: "Dahisar",
                                code: "DHI"
                            }
                        ]
                    },
                    {
                        name: "UP",
                        cities: [
                            {
                                cname:"Varanasi",
                                code: "VR"
                            },
                            {
                                cname: "Balia",
                                code: "BA"
                            }
                        ]
                    }
                ]
            },
        ];
    }
}




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

Output:

 

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


Article Tags :