DevExtreme Vue - 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.

jQuery
JavaScript
$(function () {
    $("#galleryContainer").dxGallery({
        dataSource: [{
            imageSrc: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person1.png",
            disabled: true
        }, {
            imageSrc: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person2.png",
            imageAlt: "Peter"
        }],
        height: 300
    });
});
Angular
HTML
TypeScript
<dx-gallery
    [dataSource]="galleryDataSource"
    [height]="300">
</dx-gallery>
import { DxGalleryModule } from "devextreme-angular";
// ...
export class AppComponent {
    galleryDataSource = [{
        imageSrc: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person1.png",
        disabled: true
    }, {
        imageSrc: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person2.png",
        imageAlt: "Peter"
    }];
}
@NgModule({
    imports: [
        // ...
        DxGalleryModule
    ],
    // ...
})

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

Angular
HTML
TypeScript
<dx-gallery
    [dataSource]="galleryDataSource"
    [height]="300"
    itemTemplate="item">
    <div *dxTemplate="let data of 'item'">
        <p><b>Name</b>: <span>{{data.name}}</span></p>
        <img src="{{data.path}}" alt="{{data.name}}" />
    </div>
</dx-gallery>
import { DxGalleryModule } from "devextreme-angular";
// ...
export class AppComponent {
    galleryDataSource = [
        { path: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person1.png", name: "Maria" },
        { path: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person2.png", name: "John" },
        { path: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person3.png", name: "Xavier" }
    ];
}
@NgModule({
    imports: [
        // ...
        DxGalleryModule
    ],
    // ...
})
AngularJS
HTML
JavaScript
<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>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.galleryData = [
            { path: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person1.png", name: "Maria" },
            { path: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person2.png", name: "John" },
            { path: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person3.png", name: "Xavier" }
        ];
    });
NOTE
The dx-item-alias directive specifies the variable that is used to access the item object.
Knockout
HTML
JavaScript
<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>
var viewModel = {
    galleryData: [
        { path: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person1.png", name: "Maria" },
        { path: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person2.png", name: "John" },
        { path: "https://js.devexpress.com/Content/images/doc/18_1/PhoneJS/person3.png", name: "Xavier" }
    ]
};

ko.applyBindings(viewModel);

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/18_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 3rd-Party Template Engines article.

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

See Also