User Interaction
By default, the ContextMenu appears when a user right-clicks the target element. This action corresponds to the dxcontextmenu event. If you need the ContextMenu to appear when another event is raised, assign the event's name to the showEvent property. This property can accept several events at once as well as an object.
- <dx-context-menu
- [items]="contextMenuItems"
- target="#someElement"
- showEvent="dxcontextmenu dblclick">
- </dx-context-menu>
- import { DxContextMenuModule } from 'devextreme-angular';
- // ...
- export class AppComponent {
- contextMenuItems = [
- { text: 'Zoom In', icon: 'plus' },
- { text: 'Share', icon: 'message' },
- { text: 'Download', icon: 'download' }
- ];
- }
- @NgModule({
- imports: [
- // ...
- DxContextMenuModule
- ],
- // ...
- })
The ContextMenu is hidden when a user clicks anywhere outside it. You can redefine this behavior or turn it off completely using the hideOnOutsideClick property. For example, the ContextMenu in the following code will not be hidden until its own target element is clicked.
- <dx-context-menu
- [items]="contextMenuItems"
- target="#someElement"
- (hideOnOutsideClick)="hideOnOutsideClick($event)">
- </dx-context-menu>
- import { DxContextMenuModule } from 'devextreme-angular';
- // ...
- export class AppComponent {
- contextMenuItems = [
- { text: 'Zoom In', icon: 'plus' },
- { text: 'Share', icon: 'message' },
- { text: 'Download', icon: 'download' }
- ];
- hideOnOutsideClick(e) {
- return e.target === document.getElementById('someElement');
- }
- }
- @NgModule({
- imports: [
- // ...
- DxContextMenuModule
- ],
- // ...
- })
API
The ContextMenu UI component is not supposed to be opened programmatically, but rare scenarios demand this. This article shows how to do this using the Button UI component. This choice is made for purely demonstrational purposes, and you can do the same with another UI component following the same guidelines.
To open or close the ContextMenu from code, bind the visible property of the ContextMenu to a component property. After that, change this component property, and the context menu will be opened or closed:
- <dx-button
- text="Open the context menu"
- (onClick)="isContextMenuVisible = true">
- </dx-button>
- <dx-context-menu
- [items]="contextMenuItems"
- target="#someElement"
- [(visible)]="isContextMenuVisible">
- </dx-context-menu>
- import { DxContextMenuModule, DxButtonModule } from 'devextreme-angular';
- // ...
- export class AppComponent {
- contextMenuItems = [
- { text: 'Zoom In', icon: 'plus' },
- { text: 'Share', icon: 'message' },
- { text: 'Download', icon: 'download' }
- ];
- isContextMenuVisible = false;
- }
- @NgModule({
- imports: [
- // ...
- DxContextMenuModule,
- DxButtonModule
- ],
- // ...
- })
When invoking the context menu from code, you may want to specify its position.
- <dx-context-menu ...
- target="#someElement"
- [position]="contextMenuPosition">
- </dx-context-menu>
- import { ..., ViewChild } from "@angular/core";
- import { DxContextMenuModule, DxButtonModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- contextMenuPosition = {
- my: 'top right',
- at: 'top left'
- };
- }
- @NgModule({
- imports: [
- // ...
- DxContextMenuModule,
- DxButtonModule
- ],
- // ...
- })
This configuration of the position property reads as follows: "place my top right corner at the top left corner of the target element".
Events
To execute certain commands before or after the ContextMenu was opened/closed, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the UI component, assign it to the corresponding onEventName property when you configure the UI component.
- <dx-context-menu ...
- (onShowing)="handleShowingEvent($event)"
- (onShown)="handleShownEvent($event)"
- (onHiding)="handleHidingEvent($event)"
- (onHidden)="handleHiddenEvent($event)">
- </dx-context-menu>
- import { DxContextMenuModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- // ...
- handleShowingEvent(e) {
- // Handler of the 'showing' event
- }
- handleShownEvent(e) {
- // Handler of the 'shown' event
- }
- handleHidingEvent(e) {
- // Handler of the 'hiding' event
- }
- handleHiddenEvent(e) {
- // Handler of the 'hidden' event
- }
- }
- @NgModule({
- imports: [
- // ...
- DxContextMenuModule
- ],
- // ...
- })
See Also
If you have technical questions, please create a support ticket in the DevExpress Support Center.