DevExtreme Vue - Customize Item Appearance

For a minor customization of Menu items, you can use the default item template. This template defines the appearance of an item depending on whether specific fields are present or absent from the item's data object. For example, the following code generates two root items with two drop-down menu items each. The root items are supplied with icons.

jQuery
JavaScript
var menuItems = [{
    text: "Upload", icon: "upload",
    items: [
        { text: "From your computer" },
        { text: "From a cloud service" }
    ]
}, {
    text: "Share", icon: "message",
    items: [
        { text: "Log in with Facebook" },
        { text: "Log in with Twitter" }
    ]
}];

$(function () {
    $("#menuContainer").dxMenu({
        items: menuItems
    });
});
Angular
HTML
TypeScript
<dx-menu
    [items]="menuItems">
</dx-menu>
import { DxMenuModule } from "devextreme-angular";
// ...
export class AppComponent {
    menuItems = [{
        text: "Upload", icon: "upload",
        items: [
            { text: "From your computer" },
            { text: "From a cloud service" }
        ]
    }, {
        text: "Share", icon: "message",
        items: [
            { text: "Log in with Facebook" },
            { text: "Log in with Twitter" }
        ]
    }];
}
@NgModule({
    imports: [
        // ...
        DxMenuModule
    ],
    // ...
})

Using the default item template is the easiest way to customize an item, but it lacks flexibility. Instead, you can define a custom template. For Angular, AngularJS, and Knockout apps, DevExtreme provides a markup component called dxTemplate. The following code gives a simple example of how you can use dxTemplate to customize menu items.

Angular
HTML
TypeScript
<dx-menu
    [items]="menuItems"
    itemTemplate="items">
    <div *dxTemplate="let item of 'items'">
        <i>{{item.text}}</i>
    </div>
</dx-menu>
import { DxMenuModule } from "devextreme-angular";
// ...
export class AppComponent {
    menuItems = [{
        text: "Upload",
        items: [
            { text: "From your computer" },
            { text: "From a cloud service" }
        ]
    }, {
        text: "Share",
        items: [
            { text: "Log in with Facebook" },
            { text: "Log in with Twitter" }
        ]
    }];
}
@NgModule({
    imports: [
        // ...
        DxMenuModule
    ],
    // ...
})
AngularJS
JavaScript
HTML
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.menuItems = [{
            text: "Upload",
            items: [
                { text: "From your computer" },
                { text: "From a cloud service" }
            ]
        }, {
            text: "Share",
            items: [
                { text: "Log in with Facebook" },
                { text: "Log in with Twitter" }
            ]
        }];
    });
<div ng-controller="DemoController">
    <div dx-menu="{ items: menuItems, itemTemplate: 'items' }" dx-item-alias="item">
        <div data-options="dxTemplate: { name: 'items' }">
            <i>{{ item.text }}</i>
        </div>
    </div>
</div>
NOTE
The dx-item-alias directive specifies the variable that is used to access the item object.
Knockout
JavaScript
HTML
var viewModel = {
    menuItems: [{
        text: "Upload",
        items: [
            { text: "From your computer" },
            { text: "From a cloud service" }
        ]
    }, {
        text: "Share",
        items: [
            { text: "Log in with Facebook" },
            { text: "Log in with Twitter" }
        ]
    }]
};

ko.applyBindings(viewModel);
<div data-bind="dxMenu: { items: menuItems, itemTemplate: 'items' }">
    <div data-options="dxTemplate: { name: 'items' }">
        <i data-bind="text: text"></i>
    </div>
</div>

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

JavaScript
var menuItems = [{
    text: "Upload",
    items: [
        { text: "From your computer" },
        { text: "From a cloud service" }
    ]
}, {
    text: "Share",
    items: [
        { text: "Log in with Facebook" },
        { text: "Log in with Twitter" }
    ]
}];

$(function () {
    $("#menuContainer").dxMenu({
        items: menuItems,
        itemTemplate: function (itemData, itemIndex, itemElement) {
            itemElement.append("<i>" + itemData.text + "</i>");
        }
    });
});

You can also customize an individual 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 menuItems = [{
    text: "Upload", icon: "upload",
    template: $("#individualTemplate"),
    items: [
        { text: "From your computer" },
        { text: "From a cloud service" }
    ]
},
    // ...
];

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

See Also