Angular PrimeNG Form CascadeSelect Basic 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 TreeTable Column Resize in Angular PrimeNG.
Angular PrimeNG Form CascadeSelect Basic 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 Basic Properties:
- options: This property is used to define an array of select items to display as the available options.
- optionLabel: This property is used to label the option.
- optionGroupLabel: This property is used to label the option group.
- optionGroupChildren: This property is used to retrieve the items of the group.
Syntax:
<p-cascadeSelect [options]="..." optionLabel="..." optionGroupLabel="..." [optionGroupChildren]="..." [(ngModel)]="..."> </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: After completing the above steps, the project structure will look like the following.
Example1: In the below code, we will use the above syntax to demonstrate the use of the Form CascadeSelect Basic Component.
- app.component.html:
HTML
<!DOCTYPE html> < head > < title >GFG</ title > < style > body { background-color: lightgrey; width: 100%; height: 540px; } </ style > </ head > < html > < body > < div style = "text-align:center;" > < h1 style = "color:green;" >w3wiki</ h1 > < h3 >A computer science portal for Beginner</ h3 > < h4 >Angular PrimeNG Form CascadeSelect Component</ h4 > < h5 >Basic</ h5 > < p-cascadeSelect [(ngModel)]="selectedCity1" [options]="countries" optionLabel = "cname" optionGroupLabel = "name" [optionGroupChildren]="['states', 'cities']" [style]="{ minWidth: '14rem' }" placeholder = "Select one City" > </ p-cascadeSelect > </ div > </ body > </ html > |
- 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: "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" } ] } ] }, ]; } } |
- 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, we will use the above syntax to demonstrate the use of the Form CascadeSelect Basic 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 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: