Angular PrimeNG TieredMenu Methods
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. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we will see the Angular PrimeNG TieredMenu Methods.
The TieredMenu component allows the creation of the menu in the form of a tier. It displays submenus in nested overlays. The TieredMenu Methods are used to display or hide or toggle the visibility of the menu.
Angular PrimeNG TieredMenu Methods:
- toggle: It is used to toggle the visibility of the popup menu.
- show: It is used to display the popup menu.
- hide: It is used to hide the popup menu.
Syntax:
// For app.component.html <p-tieredMenu #menu [model]="items" [popup]="true"> </p-tieredMenu> <button #btn type="button" pButton icon="pi pi-fw pi-list" label="Show" (click)="handleClick($event)"> </button> // For app.component.ts handleClick(event) { this.menu.show(event); }
Creating Angular application & Module Installation:
Step 1: Create an Angular application using the following command.
ng new Beginner_angular
Step 2: After creating your project folder i.e. Beginner_angular, move to it using the following command.
cd Beginner_angular
Step 3: Install PrimeNG in your given directory.
npm install primeng --save npm install primeicons --save
Project Structure: The project structure will look like the following:
Example 1: In the following example, we have a TieredMenu with a show button. This calls the show() method.
- app.component.html
HTML
< h1 style = "color: green; text-align:center;" > w3wiki </ h1 > < h3 >Angular PrimeNG TieredMenu Methods</ h3 > < p-tieredMenu #menu [model]="items" [popup]="true"> </ p-tieredMenu > < button #btn type = "button" pButton icon = "pi pi-fw pi-list" label = "Show" (click)="handleClick($event)"> </ button > |
- app.component.ts
Javascript
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core' ; import { MenuItem } from 'primeng/api' ; import { SlideMenu } from 'primeng/slidemenu' ; @Component({ selector: 'app-root' , templateUrl: './app.component.html' , styleUrls: [ './app.component.css' ], }) export class AppComponent { items: MenuItem[]; @ViewChild( 'menu' ) menu!: SlideMenu; ngOnInit() { this .items = [ { label: 'Data Structures' , items: [ { label: 'Linked List' , items: [ { label: 'Singly Linked List' , }, { label: 'Doubly Linked List' , }, ], }, { label: 'Trie' , }, { label: 'Array' , }, ], }, { label: 'Algorithms' , items: [ { label: 'Sorting' , }, { label: 'Searching' , }, ], }, { label: 'Development' , items: [ { label: 'Android' , }, { label: 'Web development' , }, ], }, ]; } handleClick(event) { this .menu.show(event); } } |
- app.module.ts
Javascript
import { NgModule } from '@angular/core' ; import { BrowserModule } from '@angular/platform-browser' ; import { BrowserAnimationsModule } from '@angular/platform-browser/animations' ; import { AppComponent } from './app.component' ; import { TieredMenuModule } from 'primeng/tieredmenu' ; import { ButtonModule } from 'primeng/button' ; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, TieredMenuModule, ButtonModule ], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } |
Output:
Example 2: In the following example, we have Show Button and a Toggle button with their respective functions.
- app.component.html
HTML
< h1 style="color: green; text-align:center;"> w3wiki </ h1 > < h3 >Angular PrimeNG TieredMenu Methods</ h3 > < p-tieredMenu #menu [model]="items" [popup]="true"> </ p-tieredMenu > < button #btn type = "button" pButton icon = "pi pi-fw pi-list" label = "Show" (click)="showClick($event)"> </ button > < button #btn type = "button" pButton icon = "pi pi-fw pi-list" label = "Toggle" (click)="toggleClick($event)"> </ button > |
- app.component.ts
Javascript
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core' ; import { MenuItem } from 'primeng/api' ; import { SlideMenu } from 'primeng/slidemenu' ; @Component({ selector: 'app-root' , templateUrl: './app.component.html' , styleUrls: [ './app.component.css' ], }) export class AppComponent { items: MenuItem[]; @ViewChild( 'menu' ) menu!: SlideMenu; ngOnInit() { this .items = [ { label: 'Data Structures' , items: [ { label: 'Linked List' , items: [ { label: 'Singly Linked List' , }, { label: 'Doubly Linked List' , }, ], }, { label: 'Trie' , }, { label: 'Array' , }, ], }, { label: 'Algorithms' , items: [ { label: 'Sorting' , }, { label: 'Searching' , }, ], }, { label: 'Development' , items: [ { label: 'Android' , }, { label: 'Web development' , }, ], }, ]; } showClick(event) { this .menu.show(event); } toggleClick(event) { this .menu.toggle(event); } } |
- app.module.ts
Javascript
import { NgModule } from '@angular/core' ; import { BrowserModule } from '@angular/platform-browser' ; import { BrowserAnimationsModule } from '@angular/platform-browser/animations' ; import { AppComponent } from './app.component' ; import { TieredMenuModule } from 'primeng/tieredmenu' ; import { ButtonModule } from 'primeng/button' ; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, TieredMenuModule, ButtonModule ], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } |
Output:
Reference: http://primefaces.org/primeng/tieredmenu
Contact Us