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.

jQuery
JavaScript
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
    });
});
Angular
HTML
TypeScript
<dx-context-menu
    [items]="contextMenuItems"
    target="#someElement"
    [visible]="true">
</dx-context-menu>
import { DxContextMenuModule } from 'devextreme-angular';
// ...
export class AppComponent {
    contextMenuItems = [
        { text: 'Zoom In', icon: 'plus' },
        { text: 'Share', icon: 'message', beginGroup: true },
        { text: 'Download', icon: 'download' }
    ];
}
@NgModule({
     imports: [
         // ...
         DxContextMenuModule
     ],
     // ...
 })
Vue
App.vue
<template>
    <DxContextMenu
        :items="contextMenuItems"
        target="#someElement"
        :visible="true"
    />
</template>
<script>
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import DxContextMenu from 'devextreme-vue/context-menu';

export default {
    components: {
        DxContextMenu
    },
    data() {
        return {
            contextMenuItems: [
                { text: 'Zoom In', icon: 'plus' },
                { text: 'Share', icon: 'message', beginGroup: true },
                { text: 'Download', icon: 'download' }
            ]
        };
    }
};
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { ContextMenu } from 'devextreme-react/context-menu';

const contextMenuItems = [
    { text: 'Zoom In', icon: 'plus' },
    { text: 'Share', icon: 'message', beginGroup: true },
    { text: 'Download', icon: 'download' }
];

class App extends React.Component {
    render() {
        return (
            <ContextMenu
                items={contextMenuItems}
                target="#someElement"
                visible={true}
            />
        );
    }
}

export default App;

If you need a more flexible solution, define an itemTemplate. In Angular and Vue, you can declare it in the markup. In React, you can use a rendering function (shown in the code below) or component:

Angular
HTML
TypeScript
<dx-context-menu
    [items]="contextMenuItems"
    target="#someElement"
    itemTemplate="item"
    [visible]="true">
    <div *dxTemplate="let data of 'item'; let i = index">
        <span class="dx-icon-{{data.icon}}"></span> 
        <i style="margin-left:5px">{{data.text}}</i><span> [{{i + 1}}]</span>
    </div>
</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
     ],
     // ...
 })
Vue
App.vue
<template>
    <DxContextMenu
        :items="contextMenuItems"
        target="#someElement"
        item-template="item"
        :visible="true">
        <template #item="{ data, index }">
            <div>
                <span :class="'dx-icon-' + data.icon"></span> 
                <i :style="{marginLeft: 5}">{{data.text}}</i><span> [{{index + 1}}]</span>
            </div>
        </template>
    </DxContextMenu>
</template>
<script>
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import DxContextMenu from 'devextreme-vue/context-menu';

export default {
    components: {
        DxContextMenu
    },
    data() {
        return {
            contextMenuItems: [
                { text: "Zoom In", icon: "plus" },
                { text: "Share", icon: "message" },
                { text: "Download", icon: "download" }
            ]
        };
    }
};
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { ContextMenu } from 'devextreme-react/context-menu';

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

class App extends React.Component {
    render() {
        return (
            <ContextMenu
                items={contextMenuItems}
                target="#someElement"
                itemRender={this.renderItem}
                visible={true}
            />
        );
    }

    renderItem(data, index) {
        return (
            <div>
                <span className={`dx-icon-${data.icon}`}></span> 
                <i style={{ marginLeft: 5 }}>{data.text}</i><span> [{index + 1}]</span>
            </div>
        );
    }
}

export default App;

If you use jQuery, use DOM manipulation methods to combine the HTML markup for context menu items. To apply this markup, use the itemTemplate callback function as shown in the following code:

JavaScript
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'
    });
});

View Demo

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.

HTML
JavaScript
<script id="individualTemplate" type="text/html">
    <!-- ... -->
</script>
var contextMenuItems = [
    { text: "Zoom In" },
    { text: "Zoom Out" },
    {
        text: "Download",
        template: $("#individualTemplate")
    },
    // ...
];

In addition, you can use a 3rd-party template engine to customize UI component appearance. For more information, see the 3rd-Party Template Engines article.

See Also