ContextMenu

A context menu widget.

Included in: dx.webappjs.js, dx.all.js

The dxContextMenu widget displays a single-level or multi-level context menu that performs an action when an item is clicked.

This widget, like any other DevExtreme UI widget, can be created using one of three possible approaches: jQuery, Knockout or AngularJS. The following code demonstrates how to create the dxContextMenu widget using the Knockout approach.

HTML
<div data-bind="dxContextMenu:{
    items: menuItems,
    itemClickAction: itemClicked
}"></div>

To position the context menu against the required element, either place the context menu element within the target element, or assign the target element CSS selector to the target option.

HTML
<div id="targetElement"></div>
<div data-bind="dxContextMenu: {
    items: menuItems,
    visible: contextMenuVisible,
    target: '#targetElement'
}"></div>

The menu items are loaded from an array specified by the items configuration option.

JavaScript
var menuItems = [
    { text: "Hide" },
    { text: "Delete" },
    {
        text: "Clipboard",
        items: [
            { text: "Copy text" },
            { text: "Clear text" },
            { text: "Paste text" }
        ]
    }
];

To display widget items, a default template is applied based on the following fields of an item object.

  • disabled
    Specifies whether or not the widget item is disabled
  • icon
    Specifies a CSS class holding the icon displayed by the item
  • iconSrc
    Specifies a URL of an image displayed by the item
  • items
    Holds an array of child items; each item of this array has the same structure as the root item object
  • selectable
    Specifies whether or not a user can select the menu item
  • selected
    Specifies whether or not the item is selected
  • text
    Specifies the text inserted into the item element
  • visible
    Specifies whether or not the widget item is visible

To learn more about the fields used in the default item template, refer to the Default Item Template reference section of the widget.

At the same time, it is enough to specify a simple array as a widget data source. In this instance, a default item template represents the text bound to the current array value.

You can introduce a custom template for widget items by adding a div element with the data-options attribute set to dxTemplate. Assign item to the template name. In this instance, the template will be applied automatically. See the Customize Widget Item Appearance topic to learn more about templates.

Declare a function that processes a menu item click and assign it to the itemClickAction configuration option.

JavaScript
var menuItemClicked = function(e){
    alert(e.itemData.text + " item clicked");
};

Note that you can access the clicked item fields via the itemData field of the object passed as an argument to the itemClickAction function.

See Also
  • To learn how to create widgets in detail, refer to the Create a Widget article.
  • The dxContextMenu widget is related to the Collection Containers category. So read an overview of the features that are common for collection container widgets in the Common Tasks topic of the Collection Containers article.
Show Example:
jQuery

Configuration

An object defining configuration options for the dxContextMenu widget.

Methods

This section describes members used to manipulate the widget.

Events

This section describes events fired by this component.

Default Item Template

This section lists the data source fields that are used in a default template for context menu items.

By default, a predefined item template is applied to display context menu items. This template is based on certain fields of the data source provided for the widget. Below is the list of the fields that take part in a default item template for the dxContextMenu. 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. For details refer to the Customize Widget Item Appearance topic.