Open and Close the Context Menu

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 option. This option can accept several events at once as well as an object.

JavaScript
var contextMenuItems = [
    { text: "Zoom In", icon: "plus" },
    { text: "Share", icon: "message" },
    { text: "Download", icon: "download" }
];

$(function () {
    $("#contextMenuContainer").dxContextMenu({
        items: contextMenuItems,
        target: "#someElement",
        showEvent: "dxcontextmenu dblclick"
    });
});

The ContextMenu is closed when a user clicks anywhere outside it. You can redefine this behavior or turn it off completely using the closeOnOutsideClick option. For example, the ContextMenu in the following code will not be closed until its own target element is clicked.

JavaScript
var contextMenuItems = [
    { text: "Zoom In", icon: "plus" },
    { text: "Share", icon: "message" },
    { text: "Download", icon: "download" }
];

$(function () {
    $("#contextMenuContainer").dxContextMenu({
        items: contextMenuItems,
        target: "#someElement",
        closeOnOutsideClick: function(e){
            return e.target === $("#someElement").get()[0];
        }
    });
});

API

The ContextMenu widget is not supposed to be opened programmatically, but rare scenarios demand this. This article shows how to do this using the Button widget. This choice is made for purely demonstrational purposes, and you can do the same with another widget following the same guidelines.

See Also

To open or close the ContextMenu from code, call the show() or hide() method. You can do the same thing using the toggle(showing) method. Pass true or false to this method to open or close the context menu, respectively.

JavaScript
var contextMenuItems = [
    { text: "Zoom In", icon: "plus" },
    { text: "Share", icon: "message" },
    { text: "Download", icon: "download" }
];

$(function () {
    $("#buttonContainer").dxButton({
        text: "Open the context menu",
        onClick: function () {
            $("#contextMenuContainer").dxContextMenu("show");
            // ---------- or ----------
            $("#contextMenuContainer").dxContextMenu("toggle", true);
        }
    });

    $("#contextMenuContainer").dxContextMenu({
        items: contextMenuItems,
        target: "#someElement"
    });
});

When using AngularJS or Knockout, use a different approach. Bind the visible property of the ContextMenu widget to a scope property (in AngularJS) or an observable variable (in Knockout). After that, change this scope property or observable variable, and the context menu will be opened or closed.

AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-button="{
        text: 'Open the context menu',
        onClick: showContextMenu
    }"></div>
    <div dx-context-menu="{
        items: contextMenuItems,
        target: '#someElement',
        bindingOptions: {
            visible: 'isContextMenuVisible'
        }
    }"></div>
</div>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.contextMenuItems = [
            { text: "Zoom In", icon: "plus" },
            { text: "Share", icon: "message" },
            { text: "Download", icon: "download" }
        ];
        $scope.isContextMenuVisible = false;
        $scope.showContextMenu = function () {
            $scope.isContextMenuVisible = true;
        }
    });
Knockout
HTML
JavaScript
<div data-bind="dxButton: {
    text: 'Open the context menu',
    onClick: function (e) {
        e.model.isContextMenuVisible(true);
    }
}"></div>
<div data-bind="dxContextMenu: {
    items: contextMenuItems,
    target: '#someElement',
    visible: isContextMenuVisible
}"></div>
var viewModel = {
    contextMenuItems: [
        { text: "Zoom In", icon: "plus" },
        { text: "Share", icon: "message" },
        { text: "Download", icon: "download" }
    ],
    isContextMenuVisible: ko.observable(false)
};

ko.applyBindings(viewModel);

When invoking the context menu from code, you may want to specify its position.

JavaScript
$(function () {
    $("#buttonContainer").dxButton({
        text: "Open the context menu",
        onClick: function () {
            $("#contextMenuContainer").dxContextMenu("show");
        }
    });

    $("#contextMenuContainer").dxContextMenu({
        items: contextMenuItems,
        target: "#someElement",
        position: {
            my: "top right",
            at: "top left"
        }
    });
});

This configuration of the position option 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 widget, assign it to the corresponding onEventName option when you configure the widget.

JavaScript
$(function () {
    $("#contextMenuContainer").dxContextMenu({
        // ...
        onShowing: function (e) {
            // Handler of the "showing" event
        },
        onShown: function (e) {
            // Handler of the "shown" event
        },
        onHiding: function (e) {
            // Handler of the "hiding" event
        },
        onHidden: function (e) {
            // Handler of the "hidden" event
        }
    });
});

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method.

JavaScript
var hiddenEventHandler1 = function (e) {
    // First handler of the "hidden" event
};

var hiddenEventHandler2 = function (e) {
    // Second handler of the "hidden" event
};

$("#contextMenuContainer").dxContextMenu("instance")
    .on('hidden', hiddenEventHandler1)
    .on('hidden', hiddenEventHandler2);
See Also