DevExtreme React - Customize Menu Items

For a minor customization of menu items, you can define specific fields in item data objects. 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%;
  • }

If you need a more flexible solution, define a custom template. For AngularJS and Knockout apps, DevExtreme provides the dxTemplate markup component. The following code gives a simple example of how you can use dxTemplate to customize the items of a slide-out menu.

If you use jQuery alone, use DOM manipulation methods to combine the HTML markup for menu items. 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