MenuMenu is a navigation/command component that supports dynamic and static positioning.

Menu - 图1

Documentation

Import

  1. import {MenuModule} from 'primeng/menu';
  2. import {MenuItem} from 'primeng/api';
  3.  

MenuModel API

Menu uses the common menumodel api to define its items, visit MenuModel API for details.

Getting Started

Menu requires a collection of menuitems as its model.

  1. <p-menu [model]="items"></p-menu>
  2.  
  1. export class MenuDemo {
  2. items: MenuItem[];
  3. ngOnInit() {
  4. this.items = [
  5. {label: 'New', icon: 'pi pi-fw pi-plus'},
  6. {label: 'Open', icon: 'pi pi-fw pi-download'},
  7. {label: 'Undo', icon: 'pi pi-fw pi-refresh'}
  8. ];
  9. }
  10. }
  11.  

SubMenus

Menu supports 1 level of nesting via subitems of an item.

  1. export class MenuDemo {
  2. items: MenuItem[];
  3. ngOnInit() {
  4. this.items = [{
  5. label: 'File',
  6. items: [
  7. {label: 'New', icon: 'pi pi-fw pi-plus'},
  8. {label: 'Download', icon: 'pi pi-fw pi-download'}
  9. ]
  10. },
  11. {
  12. label: 'Edit',
  13. items: [
  14. {label: 'Add User', icon: 'pi pi-fw pi-user-plus'},
  15. {label: 'Remove User', icon: 'pi pi-fw pi-user-minus'}
  16. ]
  17. }];
  18. }
  19. }
  20.  

Popup Mode

Menu is inline by default, popup mode is also supported by enabling popup property and calling toggle method by passing the event from the anchor element.

  1. <p-menu #menu [popup]="true" [model]="items"></p-menu>
  2. <button type="button" pButton icon="fa fa-fw fa-list" label="Show" (click)="menu.toggle($event)"></button>
  3.  

Animation Configuration

Transition of the open and hide animations can be customized using the showTransitionOptions and hideTransitionOptions properties, example below disables the animations altogether.

  1. <p-menu [showTransitionOptions]="'0ms'" [hideTransitionOptions]="'0ms'" #menu [popup]="true" [model]="items"></p-menu>
  2. <button type="button" pButton icon="fa fa-fw fa-list" label="Show" (click)="menu.toggle($event)"></button>
  3.  

Properties

NameTypeDefaultDescription
modelarraynullAn array of menuitems.
popupbooleanfalseDefines if menu would displayed as a popup.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
appendToanynullTarget element to attach the overlay, valid values are "body" or a local ng-template variable of another element.
baseZIndexnumber0Base zIndex value to use in layering.
autoZIndexbooleantrueWhether to automatically manage layering.
showTransitionOptionsstring225ms ease-outTransition options of the show animation.
hideTransitionOptionsstring195ms ease-inTransition options of the hide animation.

Events

NameParametersDescription
onShowevent: Event objectCallback to invoke when overlay menu is shown.
onHideevent: Event objectCallback to invoke when overlay menu is hidden.

Methods

NameParametersDescription
toggleevent: browser eventToggles the visibility of the popup menu.
showevent: browser eventDisplays the popup menu.
hide-Hides the popup menu.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-menuContainer element.
ui-menu-listList element.
ui-menuitemMenuitem element.
ui-menuitem-textLabel of a menuitem.
ui-menuitem-iconIcon of a menuitem.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic</h3>
  2. <p-menu [model]="items"></p-menu>
  3. <h3>Popup</h3>
  4. <p-menu #menu [popup]="true" [model]="items"></p-menu>
  5. <button type="button" pButton icon="pi pi-bars" label="Show" (click)="menu.toggle($event)"></button>
  6.  
  1. export class MenuDemo {
  2. items: MenuItem[];
  3. ngOnInit() {
  4. this.items = [{
  5. label: 'File',
  6. items: [
  7. {label: 'New', icon: 'pi pi-fw pi-plus'},
  8. {label: 'Download', icon: 'pi pi-fw pi-download'}
  9. ]
  10. },
  11. {
  12. label: 'Edit',
  13. items: [
  14. {label: 'Add User', icon: 'pi pi-fw pi-user-plus'},
  15. {label: 'Remove User', icon: 'pi pi-fw pi-user-minus'}
  16. ]
  17. }];
  18. }
  19. }
  20.