Angular PrimeNG TieredMenu Component - GeeksforGeeks

08 Sep, 2021
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 know how to use the TieredMenu component in Angular PrimeNG. We will also learn about the properties, methods, styling along with their syntaxes that will be used in the code.
TieredMenu component: It allows a user to make the menu in the form of tiers.
Properties:
model: It is an array of menu items. It is of array data type & the default value is null.
popup: It defines if the menu would be displayed as a popup. It is of the boolean data type & the default value is false.
appendTo: It specifies the target element to attach the overlay & the valid values are “body” or a local ng-template variable of another element. It is of array data type & the default value is null.
style: It sets an inline style of the component. It is of string data type & the default value is null.
styleClass: It sets the style class of the component. It accepts the string data type & the default value is null.
baseZIndex: It is a base zIndex value to use in layering. It accepts the number as input data type & the default value is 0.
autoZIndex: It specifies whether to automatically manage the layering. It is of the boolean data type & the default value is true.
autoDisplay: It specifies whether to show a root submenu on mouseover. It is of the boolean data type & the default value is false.
showTransitionOptions: It shows transition options to show the animation. It accepts the string data type & the default value is .12s cubic-bezier(0, 0, 0.2, 1).
hideTransitionOptions: It shows transition options to hide the animation. It accepts the string data type & the default value is .1s linear.
Methods:
toggle: It is used to toggles the visibility of the popup menu.
show: It is used to displays the popup menu.
hide: It is used to hides the popup menu.
 
Styling:
p-tieredmenu: It is a container element.
p-menu-list: It is a list element.
p-menuitem: It is a menuitem element.
p-menuitem-text: It is a label of a menu item.
p-menuitem-icon: It is an icon of a menu item.
p-submenu-icon: It is the arrow icon of a submenu.
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 complete installation, it will look like the following:
 
Example 1: This is the basic example that shows how to use the TieredMenu component.
app.component.html
<h2>GeeksforGeeks</h2><h5>PrimeNG TieredMenu Component</h5><p-tieredMenu [model]="gfg"></p-tieredMenu>
app.component.ts
import { Component } from "@angular/core";import { MenuItem } from "primeng/api";  @Component({  selector: "my-app",  templateUrl: "./app.component.html",})export class AppComponent {  gfg: MenuItem[];    ngOnInit() {    this.gfg = [      {        label: "JavaScript",        items: [          {            label: "JavaScript1",            items: [              {                label: "JavaScript1.1",              },              {                label: "JavaScript1.2",              },            ],          },          {            label: "JavaScript2",          },          {            label: "JavaScript3",          },        ],      },      {        label: "HTML",        items: [          {            label: "HTML 1",          },          {            label: "HTML 2",          },        ],      },      {        label: "Angular",          items: [          {            label: "Angular 1",          },          {            label: "Angular 2",          },        ],      },    ];  }}
app.module.ts
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";  @NgModule({    imports: [BrowserModule,               BrowserAnimationsModule,              TieredMenuModule],    declarations: [AppComponent],    bootstrap: [AppComponent]})export class AppModule {}
Output:
Example 2: In this example, we will make the tieredmenu component using popup.
app.component.html
<h2>GeeksforGeeks</h2><h5>PrimeNG TieredMenu Component</h5><button #btn type="button" pButton label="Click Here"        (click)="menu.toggle($event)"></button><p-tieredMenu #menu [model]="gfg" [popup]="true"></p-tieredMenu>
app.component.ts
import { Component } from '@angular/core';import { MenuItem } from 'primeng/api';  @Component({  selector: 'my-app',  templateUrl: './app.component.html'})export class AppComponent {  gfg: MenuItem[];    ngOnInit() {    this.gfg = [      {        label: 'JavaScript',        items: [          {            label: 'JavaScript1',            items: [              {                label: 'JavaScript1.1'              },              {                label: 'JavaScript1.2'              }            ]          },          {            label: 'JavaScript2'          },          {            label: 'JavaScript3'          }        ]      },      {        label: 'HTML',        items: [          {            label: 'HTML 1'          },          {            label: 'HTML 2'          }        ]      },      {        label: 'Angular',          items: [          {            label: 'Angular 1'          },          {            label: 'Angular 2'          }        ]      }    ];  }}
app.module.ts
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: https://primefaces.org/primeng/showcase/#/tieredmenu
Angular-PrimeNG
AngularJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Angular PrimeNG Dropdown Component
How to make a Bootstrap Modal Popup in Angular 9/8 ?
Angular 10 (blur) Event
How to setup 404 page in angular routing ?
How to create module with Routing in Angular 9 ?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?