JavaScript/jQuery ContextMenu - Customize Item Appearance
For a minor customization of ContextMenu items, you can define specific fields in item data objects. For example, the following code generates three context menu items. Between the first and the second items lies a separator dividing one group of items from another. All the items are supplied with icons.
- var contextMenuItems = [
- { text: 'Zoom In', icon: 'plus' },
- { text: 'Share', icon: 'message', beginGroup: true },
- { text: 'Download', icon: 'download' }
- ];
- $(function() {
- $("#contextMenuContainer").dxContextMenu({
- items: contextMenuItems,
- target: "#someElement",
- visible: true
- });
- });
If you need a more flexible solution, define an itemTemplate.
- var contextMenuItems = [
- { text: "Zoom In", icon: "plus" },
- { text: "Share", icon: "message" },
- { text: "Download", icon: "download" }
- ];
- $(function() {
- $("#contextMenuContainer").dxContextMenu({
- items: contextMenuItems,
- visible: true,
- itemTemplate: function(itemData, itemIndex, itemElement) {
- var iconElement = $("<span></span>");
- iconElement.addClass("dx-icon-" + itemData.icon);
- itemElement.append(iconElement);
- itemElement.append("<i style='margin-left:5px'>" + itemData.text + "</i>" + " [" + itemIndex + "]");
- },
- target: '#someElement'
- });
- });
You can also customize an individual context menu item. For this purpose, declare a template for this item as a script and pass its id
to the template field of the item's data object.
- <script id="individualTemplate" type="text/html">
- <!-- ... -->
- </script>
- var contextMenuItems = [
- { text: "Zoom In" },
- { text: "Zoom Out" },
- {
- text: "Download",
- template: $("#individualTemplate")
- },
- // ...
- ];
See Also
If you have technical questions, please create a support ticket in the DevExpress Support Center.