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 Group Headers

By default, group headers display text of the key field in a bold font. You can define a custom template for the group headers if you need to. 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 group headers.

Angular
HTML
TypeScript
<dx-tag-box
    [dataSource]="tagBoxDataSource"
    [grouped]="true"
    groupTemplate="group"
    displayExpr="name"
    valueExpr="count">
    <div *dxTemplate="let data of 'group'">
        <p>{{data.key}} | Count: {{data.overallCount}}</p>
    </div>
</dx-tag-box>
import { DxTagBoxModule } from "devextreme-angular";
import DataSource from "devextreme/data/data_source";
// ...
export class AppComponent {
    fruitsVegetables = [{
        key: "Fruits",
        items: [
            { name: "Apples", count: 10 },
            { name: "Oranges", count: 12 },
            { name: "Lemons", count: 15 }
        ]
    }, {
        key: "Vegetables",
        items: [
            { name: "Potatoes", count: 5 },
            { name: "Tomatoes", count: 9 },
            { name: "Turnips", count: 8 }
        ]
    }];
    tagBoxDataSource: DataSource = new DataSource({
        store: this.fruitsVegetables,
        map: function(groupedItem) {
            let overallCount = 0;
            groupedItem.items.forEach(function(item) {
                overallCount += item.count;
            })
            return Object.assign(groupedItem, { overallCount: overallCount });
        }
    });
}
@NgModule({
     imports: [
         // ...
         DxTagBoxModule
     ],
     // ...
 })
AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-tag-box="{
        dataSource: tagBoxDataSource,
        grouped: true,
        groupTemplate: 'group',
        displayExpr: 'name',
        valueExpr: 'count'
    }" dx-item-alias="itemObj">
        <div data-options="dxTemplate: { name: 'group' }">
            <p>{{ itemObj.key }} | Count: {{ itemObj.overallCount }}</p>
        </div>
    </div>
</div>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function ($scope) {
        var fruitsVegetables = [{
            key: "Fruits",
            items: [
                { name: "Apples", count: 10 },
                { name: "Oranges", count: 12 },
                { name: "Lemons", count: 15 }
            ]
        }, {
            key: "Vegetables",
            items: [
                { name: "Potatoes", count: 5 },
                { name: "Tomatoes", count: 9 },
                { name: "Turnips", count: 8 }
            ]
        }];
        $scope.tagBoxDataSource = new DevExpress.data.DataSource({
            store: fruitsVegetables,
            map: function(groupedItem) {
                var overallCount = 0;
                groupedItem.items.forEach(function(item) {
                    overallCount += item.count;
                })
                return $.extend(groupedItem, { overallCount: overallCount })
            }
        });
    });
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: tagBoxDataSource,
    grouped: true,
    groupTemplate: 'group',
    displayExpr: 'name',
    valueExpr: 'count'
}">
    <div data-options="dxTemplate: { name: 'group' }">
        <p data-bind="text: key + ' | Count: ' + overallCount"></p>
    </div>
</div>
var fruitsVegetables = [{
    // ...
    // omitted for brevity
    // see the AngularJS code
}];

var viewModel = {
    tagBoxDataSource: new DevExpress.data.DataSource({
        store: fruitsVegetables,
        map: function(groupedItem) {
            var overallCount = 0;
            groupedItem.items.forEach(function(item) {
                overallCount += item.count;
            })
            return $.extend(groupedItem, { overallCount: overallCount })
        }
    })
};

ko.applyBindings(viewModel);

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

JavaScript
var fruitsVegetables = [{
    // ...
    // omitted for brevity
    // see the AngularJS code
}];

$(function() {
    $("#tagBoxContainer").dxTagBox({
        dataSource: new DevExpress.data.DataSource({
            store: fruitsVegetables,
            map: function(groupedItem) {
                var overallCount = 0;
                groupedItem.items.forEach(function(item) {
                    overallCount += item.count;
                });
                return $.extend(groupedItem, { overallCount: overallCount })
            }
        }),
        grouped: true,
        groupTemplate: function(groupData, _, groupElement) {
            groupElement.append(
                $("<p>").text(groupData.key + " | Count: " + groupData.overallCount)
            )
        },
        displayExpr: 'name',
        valueExpr: 'count'
    });
});

View Demo

In addition, you can use a 3rd-party template engine to perform the needed customizations. For more information, see the 3rd-Party Template Engines article.

See Also