JavaScript/jQuery SelectBox - Customize Group Headers

By default, group headers display text of the key field in a bold font. If you need a more flexible solution, specify groupTemplate. In Angular and Vue, you can declare it in the markup. In React, you can use a render function or component as shown in the following example:

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

JavaScript
  • const fruitsVegetables = [{
  • // ...
  • // omitted for brevity
  • // see the AngularJS code
  • }];
  •  
  • $(function() {
  • $("#selectBoxContainer").dxSelectBox({
  • dataSource: new DevExpress.data.DataSource({
  • store: fruitsVegetables,
  • map: function(groupedItem) {
  • let 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