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:
- p-cascadeselect: This styling is used to define the container element.
- p-cascadeselect-label: This styling is used to define the element to display the label of the selected option.
- p-cascadeselect-trigger: This styling is used to define the icon element.
- p-cascadeselect-panel: This styling is used to define the icon element.
- p-cascadeselect-items-wrapper: This styling is used to define the wrapper element of the items list.
- p-cascadeselect-items: This styling is used to define the list element of items.
- p-cascadeselect-item: This styling is used to define the item in the list.
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.
- app.component.html
HTML
< div style = "text-align:center;" > < h1 style = "color:green;" > w3wiki </ h1 > < h3 > A computer science portal for Beginner </ 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 > |
- app.component.ts
Javascript
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' }, ], }, ], }, ]; } } |
- app.module.ts
Javascript
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.
- app.component.html
HTML
< div style = "text-align:center;" > < h1 style = "color:green;" > w3wiki </ h1 > < h3 > A computer science portal for Beginner </ 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 > |
- app.component.ts
Javascript
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' }, ], }, ], }, ]; } } |
- app.module.ts
Javascript
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:
Contact Us