Open In App

Angular PrimeNG TieredMenu 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 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:

Methods:



 

Styling:

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.




<h2>GeeksforGeeks</h2>
<h5>PrimeNG TieredMenu Component</h5>
<p-tieredMenu [model]="gfg"></p-tieredMenu>




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",
          },
        ],
      },
    ];
  }
}




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.




<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>




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'
          }
        ]
      }
    ];
  }
}




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


Article Tags :