Open In App

Angular PrimeNG Form Dropdown Templates Component

Last Updated : 03 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 Dropdown Templates Component in Angular PrimeNG.

The Dropdown component is used to make to choose the objects from the given list of items. The Templates Component is used to define the template of the dropdown component.

Angular PrimeNG Form Dropdown Templates:

  • item: It is used to define the data of the option. 
  • group: It is used to define the group of the option.
  • selectedItem: It is used to define the selected item of the option.
  • header: It is used to define the header of the option.
  • filter: It is used to set the filter in the options. 
  • empty: It is used to find if the dropdown is empty or not.
  • emptyfilter: It is used to filter empty options.
  • footer: It is used to define the footer of the option.
  • loader: It is used to set the loader in the dropdown.

 

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 the successful installation by following the above steps, the following project structure will appear:

 

Example1: This code example describes the basic usage of the Form Dropdown Templates Component in Angular PrimeNG.

  • app.component.html

HTML




<div style="text-align:center;">
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
    <h4>
        Angular PrimeNG Form Dropdown
        Templates Component
    </h4>
    <p-dropdown [options]="cities" 
                [(ngModel)]="selectedCity1" 
                placeholder="Select a City" 
                optionLabel="name"
                [showClear]="true">
        <ng-template let-group pTemplate="header">
            Header Content 
        </ng-template>
    </p-dropdown>
</div>


  • app.component.ts

Javascript




import { Component } from '@angular/core';
import { MenuItem } from 'primeng/api';
import { SelectItem } from 'primeng/api';
import { SelectItemGroup } from 'primeng/api';
  
interface City {
    name: string;
    code: string;
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
})
export class AppComponent {
    cities: City[];
    selectedCity1: City;
    selectedCity2: City;
    selectedCity3: string;
    selectedCountry: string;
    countries: any[];
    groupedCities: SelectItemGroup[];
    items: SelectItem[];
    item: string;
  
    constructor() {
        this.cities = [
            { name: 'Mumbai', code: 'MUM' },
            { name: 'Malad', code: 'MAL' },
            { name: 'Dadar', code: 'DR' },
            { name: 'Vasai', code: 'VAS' },
            { name: 'Nallasopara', code: 'NSP' },
        ];
    }
}


  • 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 { DropdownModule } from 'primeng/dropdown';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        DropdownModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }


Output:

 

Example 2: This is another code example that describes the basic usage of the Form Dropdown Templates Component in Angular PrimeNG.

  • app.component.html

HTML




<div style="text-align:center;">
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
    <h4>
        Angular PrimeNG Form Dropdown
        Templates Component
    </h4>
    <p-dropdown [options]="cities" 
                [(ngModel)]="selectedCity1" 
                placeholder="Select a City" 
                optionLabel="name"
                [showClear]="true">
        <ng-template let-group pTemplate="footer"
            Footer Content 
        </ng-template>
    </p-dropdown>
</div>


  • app.component.ts

Javascript




import { Component } from '@angular/core';
import { MenuItem } from 'primeng/api';
import { SelectItem } from 'primeng/api';
import { SelectItemGroup } from 'primeng/api';
  
interface City {
    name: string;
    code: string;
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
})
export class AppComponent {
    cities: City[];
    selectedCity1: City;
    selectedCity2: City;
    selectedCity3: string;
    selectedCountry: string;
    countries: any[];
    groupedCities: SelectItemGroup[];
    items: SelectItem[];
    item: string;
  
    constructor() {
        this.cities = [
            { name: 'Mumbai', code: 'MUM' },
            { name: 'Malad', code: 'MAL' },
            { name: 'Dadar', code: 'DR' },
            { name: 'Vasai', code: 'VAS' },
            { name: 'Nallasopara', code: 'NSP' },
        ];
    }
}


  • 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 { DropdownModule } from 'primeng/dropdown';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        DropdownModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }


Output:

 

Reference: https://www.primefaces.org/primeng/dropdown



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads