Customize Item Appearance

Gallery items are not sctrictly images. They can contain text or other elements of your choice. For a minor customization of Gallery 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 two items: one is disabled and the other has a specified alt attribute.

JavaScript
$(function () {
    $("#galleryContainer").dxGallery({
        dataSource: [{
            imageSrc: "https://js.devexpress.com/Content/images/doc/17_1/PhoneJS/person1.png",
            disabled: true
        }, {
            imageSrc: "https://js.devexpress.com/Content/images/doc/17_1/PhoneJS/person2.png",
            imageAlt: "Peter"
        }],
        height: 300
    });
});

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 gallery items.

AngularJS
JavaScript
HTML
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.galleryData = [
            { path: "https://js.devexpress.com/Content/images/doc/17_1/PhoneJS/person1.png", name: "Maria" },
            { path: "https://js.devexpress.com/Content/images/doc/17_1/PhoneJS/person2.png", name: "John" },
            { path: "https://js.devexpress.com/Content/images/doc/17_1/PhoneJS/person3.png", name: "Xavier" }
        ];
    });
<div ng-controller="DemoController">
    <div dx-gallery="{
        dataSource: galleryData,
        height: 300,
        itemTemplate: 'item'
    }" dx-item-alias="item">
        <div data-options="dxTemplate: { name: 'item' }">
            <p><b>Name</b>: <span>{{ item.name }}</span></p>
            <img ng-attr-src="{{ item.path }}" ng-attr-alt="{{ item.name }}" />
        </div>
    </div>
</div>
NOTE
The dx-item-alias directive specifies the variable that is used to access the item object.
Knockout
JavaScript
HTML
var viewModel = {
    galleryData: [
        { path: "https://js.devexpress.com/Content/images/doc/17_1/PhoneJS/person1.png", name: "Maria" },
        { path: "https://js.devexpress.com/Content/images/doc/17_1/PhoneJS/person2.png", name: "John" },
        { path: "https://js.devexpress.com/Content/images/doc/17_1/PhoneJS/person3.png", name: "Xavier" }
    ]
};

ko.applyBindings(viewModel);
<div data-bind="dxGallery: {
    dataSource: galleryData,
    height: 300,
    itemTemplate: 'item'
}">
    <div data-options="dxTemplate: { name: 'item' }">
        <p><b>Name</b>: <span data-bind="text: name"></span></p>
        <img data-bind="attr: { src: path, alt: name }" />
    </div>
</div>

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

JavaScript
$(function () {
    $("#galleryContainer").dxGallery({
        dataSource: galleryData,
        height: 300,
        itemTemplate: function(e){
            e.itemElement.empty();
            e.itemElement.append("<p><b>Name</b>: " + e.itemData.name + "</p>");
            e.itemElement.append("<img src=\"" + e.itemData.path + "\" alt=\"" + e.itemData.name + "\"/>");
        }
    });
});

You can also customize an individual Gallery item. For this purpose, declare a template for this item as a script and pass its id to the template field of the item's data object.

HTML
JavaScript
<script id="individualTemplate" type="text/html">
    <!-- ... -->
</script>
var galleryData = [{
    imageSrc: "https://js.devexpress.com/Content/images/doc/17_1/PhoneJS/person1.png",
    imageAlt: "Maria",
    template: $("#individualTemplate"),
},
    // ...
];

In addition, you can use a 3rd-party template engine to customize widget appearance. For more information, see the Use an Alternative Template Engine article.

View Built-In Template Engine Demo View 3rd-Party Template Engine Demo

See Also