ContextMenu

The ContextMenu widget displays a single- or multi-level context menu. An end user invokes this menu by a right click or a long press.

Included in: dx.web.js, dx.viz-web.js, dx.all.js
Module: ui/context_menu
Export: default
ContextMenu interactive configuration
Copy Code
Copy to Codepen
$("#contextmenu").dxContextMenu({
});

                    

DevExtreme widgets are integrated with many popular libraries and frameworks. See the Installation section (for JavaScript libraries) or the Prerequisites and Installation section (for ASP.NET MVC framework) to find details on setting up DevExtreme with a particular library or framework.

The following code shows how to create the ContextMenu widget using every supported library and framework. For more details on working with widgets in these libraries and frameworks, see the Widget Basics topic for jQuery, Angular, AngularJS, Knockout or ASP.NET MVC.

jQuery
JavaScript
HTML
$(function () {
    $("#contextMenu").dxContextMenu({
        items: [
            { text: "Hide" },
            { text: "Delete" },
            {
                text: "Clipboard",
                items: [
                    { text: "Copy" },
                    { text: "Clear" },
                    { text: "Paste" }
                ]
            }
        ],
        target: "#targetElement"
    });
});
<div id="targetElement"></div>
<div id="contextMenu"></div>
Angular
HTML
TypeScript
<div id="targetElement"></div>
<dx-context-menu
    [items]="contextMenuItems"
    target="#targetElement">
</dx-context-menu>
import { DxContextMenuModule } from 'devextreme-angular';
// ...
export class AppComponent {
    contextMenuItems = [
        { text: "Hide" },
        { text: "Delete" },
        {
            text: "Clipboard",
            items: [
                { text: "Copy" },
                { text: "Clear" },
                { text: "Paste" }
            ]
        }
    ];
}
@NgModule({
    imports: [
        // ...
        DxContextMenuModule
    ],
    // ...
})
AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div id="targetElement"></div>
    <div dx-context-menu="{
        items: contextMenuItems,
        target: '#targetElement'
    }"></div>
</div>
angular.module('DemoApp', ['dx'])
    .controller("DemoController", function ($scope) {
        $scope.contextMenuItems = [
            { text: "Hide" },
            { text: "Delete" },
            {
                text: "Clipboard",
                items: [
                    { text: "Copy" },
                    { text: "Clear" },
                    { text: "Paste" }
                ]
            }
        ];
    });
Knockout
HTML
JavaScript
<div id="targetElement"></div>
<div data-bind="dxContextMenu: {
    items: contextMenuItems,
    target: '#targetElement'
}"></div>
var viewModel = {
    contextMenuItems: [
        { text: "Hide" },
        { text: "Delete" },
        {
            text: "Clipboard",
            items: [
                { text: "Copy" },
                { text: "Clear" },
                { text: "Paste" }
            ]
        }
    ]
};
ko.applyBindings(viewModel);
ASP.NET MVC Controls
Razor C#
Razor VB
@(Html.DevExtreme().ContextMenu()
    .ID("contextMenu")
    .Target("#targetElement")
    .Items(items => {
        items.Add().Text("Hide");
        items.Add().Text("Delete");
        items.Add().Text("Clipboard").Items(clipboardItems => {
            clipboardItems.Add().Text("Copy");
            clipboardItems.Add().Text("Clear");
            clipboardItems.Add().Text("Paste");
        });
    })
)
<div id="targetElement"></div>
@(Html.DevExtreme().ContextMenu() _
    .ID("contextMenu") _
    .Target("#targetElement") _
    .Items(Sub(items)
        items.Add().Text("Hide")
        items.Add().Text("Delete")
        items.Add().Text("Clipboard").Items(Sub(clipboardItems)
            clipboardItems.Add().Text("Copy")
            clipboardItems.Add().Text("Clear")
            clipboardItems.Add().Text("Paste")
        End Sub)
    End Sub)
)
<div id="targetElement"></div>

View Demo

See Also

Configuration

An object defining configuration options for the ContextMenu widget.

Name Description
accessKey

Specifies the shortcut key that sets focus on the widget.

activeStateEnabled

A Boolean value specifying whether or not the widget changes its state when interacting with a user.

animation

Configures widget visibility animations. This object contains two fields: show and hide.

closeOnOutsideClick

Specifies whether to close the ContextMenu if a user clicks outside it.

cssClass

Specifies the name of the CSS class to be applied to the root menu level and all submenus.

dataSource

A data source used to fetch data to be displayed by the widget.

disabled

Specifies whether the widget responds to user interaction.

disabledExpr

Specifies the name of the data source item field whose value defines whether or not the corresponding widget item is disabled.

displayExpr

Specifies the name of the data source item field whose value is displayed by the widget.

elementAttr

Specifies the attributes to be attached to the widget's root element.

focusStateEnabled

Specifies whether the widget can be focused using keyboard navigation.

height

Specifies the widget's height.

hint

Specifies text for a hint that appears when a user pauses on the widget.

hoverStateEnabled

Specifies whether the widget changes its state when a user pauses on it.

items

Holds an array of menu items.

itemsExpr

Specifies which data field contains nested items.

itemTemplate

Specifies a custom template for items.

onContentReady

A handler for the contentReady event. Executed when the widget's content is ready. This handler may be executed multiple times during the widget's lifetime depending on the number of times its content changes.

onDisposing

A handler for the disposing event. Executed when the widget is removed from the DOM using the remove(), empty(), or html() jQuery methods only.

onHidden

A handler for the hidden event.

onHiding

A handler for the hiding event.

onInitialized

A handler for the initialized event. Executed only once, after the widget is initialized.

onItemClick

A handler for the itemClick event.

onItemContextMenu

A handler for the itemContextMenu event.

onItemRendered

A handler for the itemRendered event.

onOptionChanged

A handler for the optionChanged event. Executed after an option of the widget is changed.

onPositioning

A handler for the positioning event.

onSelectionChanged

A handler for the selectionChanged event. Raised after an item is selected or unselected.

onShowing

A handler for the showing event.

onShown

A handler for the shown event.

position

An object defining widget positioning options.

rtlEnabled

Switches the widget to a right-to-left representation.

selectByClick

Specifies whether or not an item becomes selected if a user clicks it.

selectedExpr

Specifies the name of the data source item field whose value defines whether or not the corresponding widget items is selected.

selectedItem

The selected item object.

selectionMode

Specifies the selection mode supported by the menu.

showEvent

Specifies options for displaying the widget.

showSubmenuMode

Specifies options of submenu showing and hiding.

submenuDirection

Specifies the direction at which submenus are displayed.

tabIndex

Specifies the number of the element when the Tab key is used for navigating.

target

The target element associated with the context menu.

visible

A Boolean value specifying whether or not the widget is visible.

width

Specifies the widget's width.

Methods

This section describes members used to manipulate the widget.

Name Description
beginUpdate()

Prevents the widget from refreshing until the endUpdate() method is called.

defaultOptions(rule)

Specifies the device-dependent default configuration options for this component.

dispose()

Removes the widget from the DOM.

element()

Gets the root widget element.

endUpdate()

Refreshes the widget after a call of the beginUpdate() method.

focus()

Sets focus on the widget.

getDataSource()

Gets the DataSource instance.

getInstance(element)

Gets the instance of a widget found using its DOM node.

hide()

Hides the widget.

instance()

Gets the widget's instance. Use it to access other methods of the widget.

off(eventName)

Detaches all event handlers from a single event.

off(eventName, eventHandler)

Detaches a particular event handler from a single event.

on(eventName, eventHandler)

Subscribes to an event.

on(events)

Subscribes to events.

option()

Gets all widget options.

option(optionName)

Gets the value of a single option.

option(optionName, optionValue)

Updates the value of a single option.

option(options)

Updates the values of several options.

registerKeyHandler(key, handler)

Registers a handler to be executed when a user presses a specific key.

repaint()

Repaints the widget. Call it if you made modifications that changed the widget's state to invalid.

selectItem(itemElement)

Selects an item found using its DOM node.

show()

Shows the widget.

toggle(showing)

Shows or hides the widget depending on the argument.

unselectItem(itemElement)

Cancels the selection of an item found using its DOM node.

Events

This section describes events fired by this widget.

Name Description
contentReady

Raised when the widget's content is ready.

disposing

Raised when the widget is removed from the DOM using the remove(), empty(), or html() jQuery methods only.

hidden

Fires when the context menu is hidden.

hiding

Fires before the context menu is hidden.

initialized

Raised only once, after the widget is initialized.

itemClick

Fires when a widget item is clicked.

itemContextMenu

Fires when the right mouse button is clicked when the pointer is over a widget item, or when the "sustained" (also known as a long press) touch action is performed.

itemRendered

Fires after a collection item is rendered.

optionChanged

Raised after a widget option is changed.

positioning

Fires before the context menu is positioned.

selectionChanged

Fires after a collection item is selected or unselected.

showing

Fires before the context menu is displayed.

shown

Fires when the context menu is displayed.

See Also

Default Item Template

This section lists the data source fields that are used in a default item template.

Type:

Object

By default, a predefined item template is applied to display the items of this widget. This template is based on certain fields of the data source provided for this widget. Below is the list of these fields. If the default item template is not appropriate for your task, implement a custom item template based on these or other fields of your data source.

See Also