DevExtreme jQuery/JS - Customize Item Appearance

For a minor customization of Accordion panels, you can use the default item template. This template defines the appearance of a panel depending on whether specific fields are present or absent from the panel's data object. For example, the following code generates three panels, the first and third are not customized, the second is disabled.

JavaScript
var accordionData = [{
    title: "Employee",
    text: "John Smith"
}, {
    title: "Phone Number",
    text: "(555)555-5555",
    disabled: true
}, {
    title: "Position",
    text: "Network Administrator"
}];

$(function () { 
    $("#accordionContainer").dxAccordion({
        dataSource: accordionData
    });
});

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 shows how you can use dxTemplate to define templates for items and item titles.

Angular
HTML
TypeScript
<dx-accordion
    [dataSource]="accordionData"
    itemTitleTemplate="title"
    itemTemplate="item">
    <div *dxTemplate="let employee of 'title'">
        <span>{{employee.firstName}}</span>
        <span>{{employee.lastName}}</span>
    </div>
    <div *dxTemplate="let employee of 'item'">
        <span>{{employee.birthDate}}</span>
        <span>{{employee.position}}</span>
    </div>
</dx-accordion>
import { DxAccordionModule } from "devextreme-angular";
// ...
export class AppComponent {
    accordionData = [{
        firstName: "John", lastName: "Smith",
        birthDate: "1986/03/14",
        position: "Network Administrator"
    }, {
        firstName: "Samantha", lastName: "Jones",
        birthDate: "1972/11/13",
        position: "Technical Writer"
    }, {
        // ...
    }];
}
@NgModule({
    imports: [
        // ...
        DxAccordionModule
    ],
    // ...
})
AngularJS
JavaScript
HTML
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.accordionData = [{
            firstName: "John", lastName: "Smith",
            birthDate: "1986/03/14",
            position: "Network Administrator"
        }, {
            firstName: "Samantha", lastName: "Jones",
            birthDate: "1972/11/13",
            position: "Technical Writer"
        }, {
            // ...
        }];
    });
<div ng-controller="DemoController">
    <div dx-accordion="{
        dataSource: accordionData,
        itemTemplate: 'item',
        itemTitleTemplate: 'title'
    }" dx-item-alias="employee">
        <div data-options="dxTemplate: { name: 'title' }">
            <span>{{ employee.firstName }}</span>
            <span>{{ employee.lastName }}</span>
        </div>
        <div data-options="dxTemplate: { name: 'item' }">
            <p>{{ employee.birthDate }}</p>
            <p>{{ employee.position }}</p>
        </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= {
    accordionData: [{
        firstName: "John", lastName: "Smith",
        birthDate: "1986/03/14",
        position: "Network Administrator"
    }, {
        firstName: "Samantha", lastName: "Jones",
        birthDate: "1972/11/13",
        position: "Technical Writer"
    }, {
        // ...
    }]
};

ko.applyBindings(viewModel);
<div data-bind="dxAccordion: {
    dataSource: accordionData,
    itemTemplate: 'item',
    itemTitleTemplate: 'title'
}">
    <div data-options="dxTemplate: { name: 'title' }">
        <span data-bind="text: firstName"></span>
        <span data-bind="text: lastName"></span>
    </div>
    <div data-options="dxTemplate: { name: 'item' }">
        <p data-bind="text: birthDate"></p>
        <p data-bind="text: position"></p>
    </div>
</div>

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

JavaScript
var accordionData = [{
    firstName: "John", lastName: "Smith",
    birthDate: "1986/03/14",
    position: "Network Administrator"
}, {
    firstName: "Samantha", lastName: "Jones",
    birthDate: "1972/11/13",
    position: "Technical Writer"
}, {
    // ...
}];

$(function () {
    $("#accordionContainer").dxAccordion({
        dataSource: accordionData,
        itemTemplate: function (itemData, itemIndex, itemElement) {
            itemElement.append("<p>" + itemData.birthDate + "</p>");
            itemElement.append("<p>" + itemData.position + "</p>");
        },
        itemTitleTemplate: function (itemData, itemIndex, itemElement) {
            itemElement.append("<span>" + itemData.firstName + "</span> ");
            itemElement.append("<span>" + itemData.lastName + "</span>");
        }
    });
});

View Demo

You can also customize an individual 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 accordionData = [{
    firstName: "John", lastName: "Smith",
    birthDate: "1986/03/14",
    position: "Network Administrator",
    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.

See Also