DevExtreme jQuery/JS - Customize Item Appearance

For a minor customization of Autocomplete 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 three items: the first is not customized, the second is disabled and the third is hidden.

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 items of the widget. For Angular, 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 the Autocomplete items.

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

JavaScript
  • var autocompleteData = [
  • { country: "Afghanistan", capital: "Kabul" },
  • { country: "Albania", capital: "Tirana" },
  • // ...
  • ];
  •  
  • $(function() {
  • $("#autocompleteContainer").dxAutocomplete({
  • dataSource: autocompleteData,
  • valueExpr: 'country',
  • placeholder: 'Type country name...',
  • itemTemplate: function (itemData, itemIndex, itemElement) {
  • itemElement.append("<p>Country: <b>" + itemData.country + "</b></p>");
  • itemElement.append("<p style='color:grey;'>Capital: <b>" + itemData.capital + "</b></p>");
  • }
  • });
  • });

You can also customize an individual Autocomplete item. For this purpose, declare a template for this item as a script and pass its id to the template field.

HTML
JavaScript
  • <script id="individualTemplate" type="text/html">
  • <!-- ... -->
  • </script>
  • var autocompleteData = [
  • { text: "James"},
  • { text: "Joseph", 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