DevExtreme React - 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.
jQuery
$(function() { $("#autocompleteContainer").dxAutocomplete({ valueExpr: "text", dataSource: [ { text: "James" }, { text: "John", disabled: true }, { text: "Joseph", visible: false } ], placeholder: "Type first name..." }); });
Angular
import { DxAutocompleteModule } from "devextreme-angular"; // ... export class AppComponent { autocompleteData = [ { text: "James" }, { text: "John", disabled: true }, { text: "Joseph", visible: false } ] } @NgModule({ imports: [ // ... DxAutocompleteModule ], // ... })
<dx-autocomplete [dataSource]="autocompleteData" valueExpr="text" placeholder="Type first name..."> </dx-autocomplete>
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.
Angular
<dx-autocomplete [dataSource]="autocompleteData" valueExpr="country" placeholder="Type country name..." itemTemplate="full"> <div *dxTemplate="let itemObj of 'full'"> <p>Country: <b>{{itemObj.country}}</b></p> <p style="color:grey;">Capital: <b>{{itemObj.capital}}</b></p> </div> </dx-autocomplete>
import { DxAutocompleteModule } from "devextreme-angular"; // ... export class AppComponent { autocompleteData = [ { country: "Afghanistan", capital: "Kabul" }, { country: "Albania", capital: "Tirana" }, // ... ] } @NgModule({ imports: [ // ... DxAutocompleteModule ], // ... })
AngularJS
<div ng-controller="DemoController"> <div dx-autocomplete="{ dataSource: autocompleteData, valueExpr: 'country', placeholder: 'Type country name...', itemTemplate: 'full' }" dx-item-alias="itemObj"> <div data-options="dxTemplate: { name: 'full' }"> <p>Country: <b>{{itemObj.country}}</b></p> <p style="color:grey;">Capital: <b>{{itemObj.capital}}</b></p> </div> </div> </div>
angular.module('DemoApp', ['dx']) .controller('DemoController', function ($scope) { $scope.autocompleteData = [ { country: "Afghanistan", capital: "Kabul" }, { country: "Albania", capital: "Tirana" }, // ... ]; });
dx-item-alias
directive specifies the variable that is used to access the item object.Knockout
<div data-bind="dxAutocomplete: { dataSource: autocompleteData, valueExpr: 'country', placeholder: 'Type country name...', itemTemplate: 'full' }"> <div data-options="dxTemplate: { name: 'full' }"> <p>Country: <b data-bind="text: country"></b></p> <p style="color:grey;">Capital: <b data-bind="text: capital"></b></p> </div> </div>
var viewModel = { autocompleteData: [ { country: "Afghanistan", capital: "Kabul" }, { country: "Albania", capital: "Tirana" }, // ... ] }; ko.applyBindings(viewModel);
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.
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.
<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
If you have technical questions, please create a support ticket in the DevExpress Support Center.