Open In App

Angular PrimeNG Form CascadeSelect Properties 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 the Angular PrimeNG Form CascadeSelect Properties 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 Component Properties:

 



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: 

 

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

npm run

Example 1: In the below code, we will make use of the above syntax to demonstrate the use of the Angular PrimeNG Form CascadeSelect Properties Component.




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Form CascadeSelect Properties Component.</h5>
  
<p-cascadeSelect
    [options]="gfg" 
    optionLabel="cname" 
    optionGroupLabel="name"
    [optionGroupChildren]="['states', 'cities']" 
    [style]="{ minWidth: '14rem' }" 
    placeholder="Select a City">
</p-cascadeSelect>




import { Component } from "@angular/core";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
export class AppComponent {
    gfg: any[] = [];
  
    ngOnInit() {
        this.gfg = [
            {
                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: "Varansi",
                                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: Below is another example code that illustrates the use of the Angular PrimeNG Form CascadeSelect Properties Component.




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Form CascadeSelect Properties Component.</h5>
  
<p-cascadeSelect p-cascadeselect-label 
    [options]="gfg" 
    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 {
    gfg: any[] = [];
  
    ngOnInit() {
        this.gfg = [
            {
                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: "Varansi",
                                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 :