DevExtreme Angular - 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.
jQuery
$(function() { $("#toolbarContainer").dxToolbar({ items: [{ widget: 'dxButton', options: { type: 'back', text: 'Back' }, location: 'before' }, { text: 'Change', locateInMenu: 'always', visible: false }, { text: 'Remove', locateInMenu: 'always', disabled: true }, { text: 'Products', location: 'center' }] }); });
Angular
<dx-toolbar> <dxi-item widget="dxButton" location="before" [options]="{ type: 'back', text: 'Back' }"> </dxi-item> <dxi-item text="Change" locateInMenu="always" [visible]="false"> </dxi-item> <dxi-item text="Remove" locateInMenu="always" [disabled]="true"> </dxi-item> <dxi-item text="Products" location="center"> </dxi-item> </dx-toolbar>
import { DxToolbarModule, DxButtonModule } from 'devextreme-angular'; // ... export class AppComponent { // ... } @NgModule({ imports: [ // ... DxToolbarModule, DxButtonModule ], // ... })
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.
Angular
<dx-toolbar [items]="items" itemTemplate="itemTemplate" menuItemTemplate="menuItemTemplate"> <div *dxTemplate="let item of 'itemTemplate'"> <b style="color:green;">{{item.text}}</b> </div> <div *dxTemplate="let menuItem of 'menuItemTemplate'"> <b style="font-style:italic;">{{menuItem.text}}</b> </div> </dx-toolbar>
import { DxToolbarModule } from 'devextreme-angular'; // ... export class AppComponent { items = [{ text: 'Back', location: 'before' }, { text: 'Change', locateInMenu: 'always' }, { text: 'Remove', locateInMenu: 'always' }, { text: 'Products', location: 'center' }]; } @NgModule({ imports: [ // ... DxToolbarModule ], // ... })
AngularJS
<div ng-controller="DemoController"> <div dx-toolbar="{ items: toolbarItems, itemTemplate: 'itemTemplate', menuItemTemplate: 'menuItemTemplate' }" dx-item-alias="itemObj"> <div data-options="dxTemplate: { name: 'itemTemplate' }"> <b style="color:green;">{{itemObj.text}}</b> </div> <div data-options="dxTemplate: { name: 'menuItemTemplate' }"> <b style="font-style:italic;">{{itemObj.text}}</b> </div> </div> </div>
angular.module('DemoApp', ['dx']) .controller('DemoController', function DemoController($scope) { $scope.toolbarItems = [{ text: 'Back', location: 'before' }, { text: 'Change', locateInMenu: 'always' }, { text: 'Remove', locateInMenu: 'always' }, { text: 'Products', location: 'center' }]; });
dx-item-alias
directive specifies the variable that is used to access the item object.Knockout
<div data-bind="dxToolbar: { items: toolbarItems, itemTemplate: 'itemTemplate', menuItemTemplate: 'menuItemTemplate' }"> <div data-options="dxTemplate: { name: 'itemTemplate' } "> <b style="color:green;" data-bind="text: text"></b> </div> <div data-options="dxTemplate: { name: 'menuItemTemplate' }"> <b style="font-style:italic;" data-bind="text: text"></b> </div> </div>
var viewModel = { toolbarItems: [{ text: 'Back', location: 'before' }, { text: 'Change', locateInMenu: 'always' }, { text: 'Remove', locateInMenu: 'always' }, { text: 'Products', location: 'center' }] }; ko.applyBindings(viewModel);
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.
$(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.
<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
If you have technical questions, please create a support ticket in the DevExpress Support Center.