Open In App

Angular PrimeNG Form CascadeSelect Styling 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. In this article, we will see how to use the  Form CascadeSelect Styling Component in Angular PrimeNG.

The CascadeSelect Styling is used to display a nested structure of options and provide different styles.



Angular PrimeNG Form CascadeSelect Styling Components:

 



Syntax:

<p-cascadeSelect>...</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:

 

Example1: In the below code example, we will make use of the above styling to demonstrate the Form CascadeSelect Styling Component.




<div style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h3>
        A computer science portal for geeks
    </h3>
    <h4>
        Angular PrimeNG Form CascadeSelect Styling Component
    </h4>
    <h5>Basic</h5>
    <p-cascadeSelect [(ngModel)]="course1" 
                     [options]="course" 
                     optionLabel="chapter" 
                     optionGroupLabel="name"
                     [optionGroupChildren]="['sub', 'chapter']" 
                     [style]="{ minWidth: '14rem' }"
                     placeholder="Select a chapter">
    </p-cascadeSelect>
</div>




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    course: any[];
    course1: any;
  
    ngOnInit() {
        this.course = [
            {
                name: 'Opertating System',
                code: 'OS',
                sub: [
                    {
                        name: 'Types of Operating System',
                        chapter: [
                            { chapter: 'Batch Operating System ' },
                            { chapter: ' Time-Sharing Operating Systems' },
                            { chapter: 'Distributed Operating System' },
                        ],
                    },
                ],
            },
        ];
    }
}




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:

 

Example2: In the below code example, we will make use of the above styling to demonstrate the Form CascadeSelect Styling Component.




<div style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h3>
        A computer science portal for geeks
    </h3>
    <h4>
        Angular PrimeNG Form CascadeSelect Styling Component
    </h4>
  
    <p-cascadeSelect p-cascadeselect-label="cname" 
                     [(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>
</div>




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    countries: any[];
    selectedCity1: any;
    selectedCity2: any;
  
    ngOnInit() {
        this.countries = [
            {
                name: 'INDIA',
                code: 'IND',
                states: [
                    {
                        name: 'UP',
                        cities: [
                            { cname: 'Varanasi', code: 'A-SY' },
                            { cname: 'Kanpur', code: 'A-NE' },
                            { cname: 'Ayodhya', code: 'A-WO' },
                        ],
                    },
                    {
                        name: 'Maharashtra',
                        cities: [
                            { cname: 'Mumbai', code: 'MUM' },
                            { cname: 'Pune', code: 'Pune' },
                        ],
                    },
                ],
            },
        ];
    }
}




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://www.primefaces.org/primeng/cascadeselect


Article Tags :