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

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: 'items',
        itemTitleTemplate: 'titles'
    }" dx-item-alias="employeeInfo">
        <div data-options="dxTemplate: { name: 'titles' }">
            <span>{{ employeeInfo.firstName }}</span>
            <span>{{ employeeInfo.lastName }}</span>
        </div>
        <div data-options="dxTemplate: { name: 'items' }">
            <p>{{ employeeInfo.birthDate }}</p>
            <p>{{ employeeInfo.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: 'items',
    itemTitleTemplate: 'titles'
}">
    <div data-options="dxTemplate: { name: 'titles' }">
        <span data-bind="text: firstName"></span>
        <span data-bind="text: lastName"></span>
    </div>
    <div data-options="dxTemplate: { name: 'items' }">
        <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 Use an Alternative Template Engine article.

See Also