All docs
V19.1
24.1
The page you are viewing does not exist in version 24.1.
23.2
The page you are viewing does not exist in version 23.2.
23.1
The page you are viewing does not exist in version 23.1.
22.2
The page you are viewing does not exist in version 22.2.
22.1
The page you are viewing does not exist in version 22.1.
21.2
The page you are viewing does not exist in version 21.2.
21.1
The page you are viewing does not exist in version 21.1.
20.2
The page you are viewing does not exist in version 20.2.
20.1
The page you are viewing does not exist in version 20.1.
19.2
19.1
18.2
18.1
17.2
A newer version of this page is available. Switch to the current version.

DevExtreme jQuery - Customize Item Appearance

For a minor customization of TagBox 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 three items: the first is not customized, the second is disabled and the third is hidden.

jQuery
JavaScript
$(function() {
    $("#tagBoxContainer").dxTagBox({
        valueExpr: 'text',
        displayExpr: 'text',
        dataSource: [
            { text: "HD Video Player" },
            { text: "SuperHD Video Player", disabled: true },
            { text: "SuperPlasma 50", visible: false }
        ],
        placeholder: "Select products..."
    });
});
Angular
TypeScript
HTML
import { DxTagBoxModule } from "devextreme-angular";
// ...
export class AppComponent {
    products =  [
        { text: "HD Video Player" },
        { text: "SuperHD Video Player", disabled: true },
        { text: "SuperPlasma 50", visible: false }
    ];
}
@NgModule({
     imports: [
         // ...
         DxTagBoxModule
     ],
     // ...
 })
<dx-tag-box
    [dataSource]="products"
    valueExpr="text"
    displayExpr="text"
    placeholder="Select products...">
</dx-tag-box>

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 widget items. For AngularJS and Knockout apps, DevExtreme provides a markup component called dxTemplate. The following code shows how you can use dxTemplate to define a template for the TagBox items.

Angular
HTML
TypeScript
<dx-tag-box
    [dataSource]="tagBoxData"
    displayExpr="name"
    valueExpr="id"
    itemTemplate="item">
    <div *dxTemplate="let data of 'item'">
        <img src="{{data.imgSrc}}" />
        <div style="display:inline-block">{{data.name}}</div>
    </div>
</dx-tag-box>
import { DxTagBoxModule } from "devextreme-angular";
// ...
export class AppComponent {
    tagBoxData = [{
        id: 1,
        name: "HD Video Player",
        imgSrc: "images/products/1-small.png"
    }, {
        id: 2,
        name: "UltraHD Player",
        imgSrc: "images/products/2-small.png"
    },
    // ...
    ];
}
@NgModule({
     imports: [
         // ...
         DxTagBoxModule
     ],
     // ...
 })
AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-tag-box="{
        dataSource: tagBoxData,
        valueExpr: 'id',
        displayExpr: 'name',
        itemTemplate: 'item'
    }" dx-item-alias="product">
        <div data-options="dxTemplate: { name: 'item' }">
            <img ng-src="{{ product.imgSrc }}" />
            <div style="display:inline-block">{{ product.name }}</div>
        </div>
    </div>
</div>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function ($scope) {
        $scope.tagBoxData = [{
            id: 1,
            name: "HD Video Player",
            imgSrc: "images/products/1-small.png"
        }, {
            id: 2,
            name: "UltraHD Player",
            imgSrc: "images/products/2-small.png"
        },
        // ...
        ];
    });
NOTE
The dx-item-alias directive specifies the variable that is used to access the item object.
Knockout
HTML
JavaScript
<div data-bind="dxTagBox: {
    dataSource: tagBoxData,
    valueExpr: 'id',
    displayExpr: 'name',
    itemTemplate: 'item'
}">
    <div data-options="dxTemplate: { name: 'item' }">
        <img data-bind="attr: { src: imgSrc }" />
        <div style="display:inline-block" data-bind="text: name"></div>
    </div>
</div>
var viewModel = {
    tagBoxData: [{
        id: 1,
        name: "HD Video Player",
        imgSrc: "images/products/1-small.png"
    }, {
        id: 2,
        name: "UltraHD Player",
        imgSrc: "images/products/2-small.png"
    },
    // ...
    ]
};

ko.applyBindings(viewModel);

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

JavaScript
var tagBoxData = [{
    id: 1,
    name: "HD Video Player",
    imgSrc: "images/products/1-small.png"
}, {
    id: 2,
    name: "UltraHD Player",
    imgSrc: "images/products/2-small.png"
},
// ...
];

$(function() {
    $("#tagBoxContainer").dxTagBox({
        dataSource: tagBoxData,
        valueExpr: 'id',
        displayExpr: 'name',
        itemTemplate: function (itemData, itemIndex, itemElement) {
            return $("<div />").append(
                $("<img />").attr("src", itemData.imgSrc),
                $("<p />").text(itemData.name)
                          .css("display", "inline-block")
            );
        }
    });
});

You can also customize an individual item in the drop-down list. For this purpose, declare a template for this item as a script and pass its id to the template field.

HTML
JavaScript
<script id="individualTemplate" type="text/html">
    <!-- ... -->
</script>
var tagBoxData = [
    { text: "SuperHD Player"},
    { text: "HD Video Player", template: $("#individualTemplate") },
    // ...
];

Using similar techniques, you can also customize tags of the selected items. The template for them should be assigned to the tagTemplate option.

jQuery
JavaScript
$(function() {
    $("#tagBoxContainer").dxTagBox({
        dataSource: tagBoxData,
        valueExpr: 'id',
        displayExpr: 'name',
        tagTemplate: function (itemData, tagElement) {
            tagElement.append("<p><b>" + itemData.name + "</b> (" + itemData.id + ")</p>");
        }
    });
});
Angular
HTML
TypeScript
<dx-tag-box
    [dataSource]="tagBoxData"
    valueExpr="id"
    displayExpr="name"
    tagTemplate="tagTemplate">
    <div *dxTemplate="let data of 'tagTemplate'">
        <p><b>{{data.name}}</b> ({{data.id}})</p>
    </div>
</dx-tag-box>
import { DxTagBoxModule } from "devextreme-angular";
// ...
export class AppComponent {
    tagBoxData = [
        { id: 1, name: "HD Video Player", imgSrc: "images/products/1-small.png" },
        // ...
    ];
}
@NgModule({
     imports: [
         // ...
         DxTagBoxModule
     ],
     // ...
 })
AngularJS
HTML
<div ng-controller="DemoController">
    <div dx-tag-box="{
        dataSource: tagBoxData,
        valueExpr: 'id',
        displayExpr: 'name',
        tagTemplate: 'tagTemplate'
    }" dx-item-alias="product">
        <div data-options="dxTemplate: { name: 'tagTemplate' }">
            <p><b>{{ product.name }}</b> ({{ product.id }})</p>
        </div>
    </div>
</div>
Knockout
HTML
<div data-bind="dxTagBox: {
    dataSource: tagBoxData,
    valueExpr: 'id',
    displayExpr: 'name',
    tagTemplate: 'tagTemplate'
}">
    <div data-options="dxTemplate: { name: 'tagTemplate' }">
        <p><b data-bind="text: name"></b> (<span data-bind="text: id"></span>)</p>
    </div>
</div>

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