DevExtreme jQuery/JS - Customize Item Appearance

For a minor customization of Toolbar 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 gererates four toolbar items: the first is a widget, the second is hidden, the third is disabled, the fourth is relocated.

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 items on the toolbar and commands on the overflow menu.

If you use jQuery alone, combine the HTML markup manually with jQuery DOM manipulation methods. To apply the markup to items on the toolbar and commands on the overflow menu, use the itemTemplate and menuItemTemplate callback functions, respectively.

JavaScript
  • $(function() {
  • $("#toolbarContainer").dxToolbar({
  • items: toolbarItems,
  • itemTemplate: function (itemData, itemIndex, itemElement) {
  • itemElement.append("<b style='color:green;'>" + itemData.text + "</b>");
  • },
  • menuItemTemplate: function (itemData, itemIndex, itemElement) {
  • itemElement.append("<b style='font-style:italic;'>" + itemData.text + "</b>");
  • }
  • });
  • });

You can also customize an individual toolbar item or menu command. For this purpose, declare a template for this item or command as a script and pass its id to the template or menu item template option, respectively.

HTML
JavaScript
  • <script id="individualItemTemplate" type="text/html">
  • <!-- ... -->
  • </script>
  •  
  • <script id="individualMenuItemTemplate" type="text/html">
  • <!-- ... -->
  • </script>
  • var toolbarItems = [{
  • text: "Back",
  • location: "before",
  • template: $("#individualItemTemplate")
  • }, {
  • text: "Change",
  • locateInMenu: "always",
  • menuItemTemplate: $("#individualMenuItemTemplate")
  • },
  • // ...
  • ];

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