DevExtreme React - Customize Menu Items

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, in the following code, "Favorites" is disabled and "Help" is hidden.

JavaScript
CSS
var menuItems = [
    { text: "Home" },
    { text: "Messages" },
    { text: "Downloads" },
    { text: "Favorites", disabled: true },
    { text: "Preferences" },
    { text: "Help", visible: false }
];

$(function () {
    $("#slideOutContainer").dxSlideOut({
        dataSource: menuItems,
        onItemClick: function (e) {
            e.component.hideMenu();
        }
    });
});
#slideOutContainer {
    height: auto;
    position: absolute;
    top: 0; 
    bottom: 0;
    width: 100%;
}

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 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 the items of a slide-out menu.

AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-slide-out="{
        dataSource: menuData,
        menuItemTemplate: 'items',
        onItemClick: hideMenu
    }" dx-item-alias="item">
        <div data-options="dxTemplate: { name: 'items' }">
            <span class="dx-icon-{{ item.icon }}"></span> 
            <span style="margin-left:10px">{{ item.text }}</span>
        </div>
    </div>
</div>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.menuData = [
            { text: "Home", icon: "home" },
            { text: "Messages", icon: "message" },
            { text: "Music", icon: "music" },
            { text: "Photos", icon: "photo" },
            { text: "Downloads", icon: "download" },
            { text: "Favorites", icon: "favorites" },
            { text: "Preferences", icon: "preferences" },
            { text: "Help", icon: "help" }
        ];
        $scope.hideMenu = function (e) {
            e.component.hideMenu();
        };
    });
NOTE
The dx-item-alias directive specifies the variable that is used to access the item object.
Knockout
HTML
JavaScript
<div data-bind="dxSlideOut: {
    dataSource: menuData,
    menuItemTemplate: 'items',
    onItemClick: function (e) {
        e.component.hideMenu();
    }
}">
    <div data-options="dxTemplate: { name: 'items' }">
        <span data-bind="css: 'dx-icon-' + icon"></span>
        <span style="margin-left:10px" data-bind="text: text"></span>
    </div>
</div>
var viewModel = {
    menuData: [
        { text: "Home", icon: "home" },
        { text: "Messages", icon: "message" },
        { text: "Music", icon: "music" },
        { text: "Photos", icon: "photo" },
        { text: "Downloads", icon: "download" },
        { text: "Favorites", icon: "favorites" },
        { text: "Preferences", icon: "preferences" },
        { text: "Help", icon: "help" }
    ]
};

ko.applyBindings(viewModel);

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

JavaScript
var menuItems = [
    { text: "Home", icon: "home" },
    { text: "Messages", icon: "message" },
    { text: "Music", icon: "music" },
    { text: "Photos", icon: "photo" },
    { text: "Downloads", icon: "download" },
    { text: "Favorites", icon: "favorites" },
    { text: "Preferences", icon: "preferences" },
    { text: "Help", icon: "help" }
];

$(function () {
    $("#slideOutContainer").dxSlideOut({
        dataSource: menuItems,
        menuItemTemplate: function (itemData, itemIndex, itemElement) {
            var iconElement = $('<span />').addClass("dx-icon-" + itemData.icon);
            var textElement = $('<span />').text(itemData.text)
                                    .attr('style', 'margin-left:10px');
            itemElement.append(iconElement, textElement);
        },
        onItemClick: function (e) {
            e.component.hideMenu();
        }
    });
});

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 menuTemplate field of the item's data object.

HTML
JavaScript
<script id="individualTemplate" type="text/html">
    <!-- ... -->
</script>
var menuItems = [
    { text: "Home" },
    { text: "Messages" },
    {
        text: "Downloads",
        menuTemplate: $("#individualTemplate")
    }
];

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