DevExtreme jQuery/JS - Templates
Templates allow you to customize DevExtreme UI components. This article gives an overview of the capabilities that DevExtreme provides for implementing and applying templates.
Default Templates
Default templates are based on data source fields. You can control appearance by adding or removing particular fields from data source objects. For example, the List UI component's default template for items contains the text, visible, and disabled fields, among others. If you assign the following array to the UI component's items or dataSource property, the first item will be disabled, the second hidden, both of them will have text, and the third item will render a custom markup:
- function customMarkup() {
- var d = document.createElement("div");
- d.innerHTML = "<i>Oranges</i>";
- return d;
- }
- var fruits = [
- { text: "Apples", disabled: true },
- { text: "Lemons", visible: false },
- { template: customMarkup }
- ];
You can achieve the same in the markup using the dxItem component that supports default and custom templates. Do not set the UI component's dataSource property in this case.
- <div id="list">
- <div data-options="dxItem: { text: 'Apples', disabled: true }"></div>
- <div data-options="dxItem: { text: 'Lemons', visible: false }"></div>
- <div data-options="dxItem: { }">
- <i>Oranges</i>
- </div>
- </div>
- $(function() {
- $("#list").dxList({/* ... */});
- });
Default templates and the fields available in them depend on the UI component. Refer to the items property description of a particular UI component for a list of template fields.
Custom Templates
Templates are passed as properties that end with ...Template. Each template has access to the following parameters:
data
A data source object or an object with fields specific to a particular template. For information on the contents ofdata
, refer to the Template Data section of the template's API reference article.index
A zero-based index of the item in the collection. Can be available only in collection UI component templates.element
A jQuery element that represents the UI component element being customized. Available only if you use jQuery.
The following code shows how to declare a template and use these parameters. This code declares an itemTemplate for the List UI component:
- $(function() {
- $("#listContainer").dxList({
- items: [
- { itemProperty: "someValue" },
- // ...
- ],
- itemTemplate: function (data, index, element) {
- return index + " - " + data.itemProperty;
- // ===== or using the "element" parameter =====
- const $item = $("<div>").text(
- index + " - " + data.itemProperty
- );
- element.append($item);
- }
- });
- });
Collection UI components are components that include the items property. These components also support templates for individual items. Do not specify the UI component's dataSource property if you use individual templates.
- $(function() {
- $("#listContainer").dxList({
- items: [{
- template: function () {
- return $("<i>").text("Item 1")
- }
- }, {
- template: function () {
- return $("<b>").text("Item 2")
- }
- },{
- template: function () {
- return $("<div>").append(
- $("<span>").text("Item with nested component"),
- $("<div>").dxButton({
- text: "Click me"
- })
- )
- }
- }]
- });
- });
3rd-Party Template Engines
You can use a 3rd-party template engine of your choice. The example below shows how to set up the Mustache template engine. Call the DevExpress.setTemplateEngine(options) method and implement functions that compile
and render
templates:
- DevExpress.setTemplateEngine({
- compile: (element) => $(element).html(),
- render: (template, data) => Mustache.render(template, data),
- });
- $(function() {
- $("#list").dxList({
- // ...
- itemTemplate: $("#itemTemplate")
- });
- })
- <div id="list"></div>
- <script type="text/html" id="itemTemplate">
- <!-- your Mustache template -->
- </script>
If you assign a function that returns a string to a template, the compile
callback receives a text node as an argument. In this case, do the following:
Implement additional logic that determines if an argument is a template element or a text node.
If an argument is a template element, call
$(element).html()
.Otherwise, append the node to an element:
$('<div>').append(element).html()
.
If you have technical questions, please create a support ticket in the DevExpress Support Center.