ContextMenuContextMenu displays an overlay menu on right click of its target. This page has two menus, one for the document and one for the image. Note that components like DataTable has special integration with ContextMenu. Refer to documentation of the individual documentation of the components having a special integration.

ContextMenu - 图1

Documentation

Import

  1. import {ContextMenuModule} from 'primeng/contextmenu';
  2. import {MenuItem} from 'primeng/api';
  3.  

MenuModel API

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

Getting Started

ContextMenu requires nested menuitems as its model and in its simplest form ContextMenu is attached to the document with global setting. .

  1. <p-contextMenu [global]="true" [model]="items"></p-contextMenu>
  2.  

Target

ContextMenu can be attached to a particular element whose local template variable name is defined using the target property.

  1. <p-contextMenu [target]="img" [model]="items2" ></p-contextMenu>
  2. <img #img src="assets/showcase/images/primeng.svg" alt="Logo">
  3.  

Exclusive Integrations

Some components like DataTable require special attention so they provide a different method to attach a context menu. Refer to individual documentation of components with special integration like DataTable.

  1. export class ContextMenuDemo {
  2. private items: MenuItem[];
  3. ngOnInit() {
  4. this.items = [
  5. {
  6. label: 'File',
  7. items: [{
  8. label: 'New',
  9. icon: 'pi pi-fw pi-plus',
  10. items: [
  11. {label: 'Project'},
  12. {label: 'Other'},
  13. ]
  14. },
  15. {label: 'Open'},
  16. {label: 'Quit'}
  17. ]
  18. },
  19. {
  20. label: 'Edit',
  21. icon: 'pi pi-fw pi-pencil',
  22. items: [
  23. {label: 'Delete', icon: 'pi pi-fw pi-trash'},
  24. {label: 'Refresh', icon: 'pi pi-fw pi-refresh'}
  25. ]
  26. }
  27. ];
  28. }
  29. }
  30.  

Properties

NameTypeDefaultDescription
modelarraynullAn array of menuitems.
globalbooleanfalseAttaches the menu to document instead of a particular item.
targetstringnullLocal template variable name of the element to attach the context menu.
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.
triggerEventstringcontextmenuEvent for which the menu must be displayed.

Methods

NameParametersDescription
toggleevent (optional): mouse 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-contextmenuContainer element.
ui-menu-listList element.
ui-menuitemMenuitem element.
ui-menuitem-textLabel of a menuitem.
ui-menuitem-iconIcon of a menuitem.
ui-submenu-iconArrow icon of a submenu.

Dependencies

None.

Source

View on GitHub

  1. <p-contextMenu [global]="true" [model]="items1"></p-contextMenu>
  2. <p-contextMenu [target]="img" [model]="items2"></p-contextMenu>
  3. <img #img src="assets/showcase/images/primeng-text.svg" alt="Logo">
  4.  
  1. export class ContextMenuDemo {
  2. items: MenuItem[];
  3. items2: MenuItem[];
  4. ngOnInit() {
  5. this.items1 = [
  6. {
  7. label: 'File',
  8. icon: 'pi pi-fw pi-file',
  9. items: [{
  10. label: 'New',
  11. icon: 'pi pi-fw pi-plus',
  12. items: [
  13. {label: 'Project'},
  14. {label: 'Other'},
  15. ]
  16. },
  17. {label: 'Open'},
  18. {separator:true},
  19. {label: 'Quit'}
  20. ]
  21. },
  22. {
  23. label: 'Edit',
  24. icon: 'pi pi-fw pi-pencil',
  25. items: [
  26. {label: 'Delete', icon: 'pi pi-fw pi-trash'},
  27. {label: 'Refresh', icon: 'pi pi-fw pi-refresh'}
  28. ]
  29. },
  30. {
  31. label: 'Help',
  32. icon: 'pi pi-fw pi-question',
  33. items: [
  34. {
  35. label: 'Contents'
  36. },
  37. {
  38. label: 'Search',
  39. icon: 'pi pi-fw pi-search',
  40. items: [
  41. {
  42. label: 'Text',
  43. items: [
  44. {
  45. label: 'Workspace'
  46. }
  47. ]
  48. },
  49. {
  50. label: 'File'
  51. }
  52. ]}
  53. ]
  54. },
  55. {
  56. label: 'Actions',
  57. icon: 'pi pi-fw pi-cog',
  58. items: [
  59. {
  60. label: 'Edit',
  61. icon: 'pi pi-fw pi-pencil',
  62. items: [
  63. {label: 'Save', icon: 'pi pi-fw pi-save'},
  64. {label: 'Update', icon: 'pi pi-fw pi-save'},
  65. ]
  66. },
  67. {
  68. label: 'Other',
  69. icon: 'pi pi-fw pi-tags',
  70. items: [
  71. {label: 'Delete', icon: 'pi pi-fw pi-minus'}
  72. ]
  73. }
  74. ]
  75. },
  76. {separator:true},
  77. {
  78. label: 'Quit', icon: 'pi pi-fw pi-times'
  79. }
  80. ];
  81. this.items2 = [
  82. {
  83. label: 'Next',
  84. icon: 'pi pi-fw pi-chevron-right'
  85. },
  86. {
  87. label: 'Prev',
  88. icon: 'pi pi-fw pi-chevron-left'
  89. }
  90. ];
  91. }
  92. }
  93.