JavaScript/jQuery List - Customize Item Appearance

For a minor customization of List items, you can define specific fields in item data objects. For example, the following code generates three items: each item has a badge, the second is disabled and the third is hidden.

jQuery
JavaScript
const fruits = [
    { text: "Apples", badge: 10 },
    { text: "Oranges", badge: 12, disabled: true },
    { text: "Lemons", badge: 15, visible: false }
];

$(function() {
    $("#listContainer").dxList({
        dataSource: fruits
    });
});
Angular
TypeScript
HTML
import { DxListModule } from "devextreme-angular";
// ...
export class AppComponent {
    fruits = [
        { text: "Apples", badge: 10 },
        { text: "Oranges", badge: 12, disabled: true },
        { text: "Lemons", badge: 15, visible: false }
    ];
}
@NgModule({
    imports: [
        // ...
        DxListModule
    ],
    // ...
})
<dx-list
    [dataSource]="fruits">
</dx-list>

If you need a more flexible solution, define a custom template for UI component items. For Angular, AngularJS, and Knockout apps, DevExtreme provides the dxTemplate markup component. The following code shows how to use dxTemplate to define a template for List items.

Angular
HTML
TypeScript
<dx-list
    [dataSource]="fruits"
    itemTemplate="listItem">
    <div *dxTemplate="let item of 'listItem'">
        <b>{{item.name}}</b><br />
        <p style="margin:0px">{{item.count}}</p>
    </div>
</dx-list>
import { DxListModule } from "devextreme-angular";
// ...
export class AppComponent {
    fruits = [
        { name: "Apples", count: 10 },
        { name: "Oranges", count: 12 },
        { name: "Lemons", count: 15 },
        { name: "Pears", count: 20 },
        { name: "Pineapples", count: 3 }
    ];
}
@NgModule({
    imports: [
        // ...
        DxListModule
    ],
    // ...
})
AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-list="{
        dataSource: fruits,
        itemTemplate: 'listItem'
    }" dx-item-alias="fruit">
        <div data-options="dxTemplate: { name: 'listItem' }">
            <b>{{ fruit.name }}</b><br />
            <p style="margin:0px">{{ fruit.count }}</p>
        </div>
    </div>
</div>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function ($scope) {
        $scope.fruits = [
            { name: "Apples", count: 10 },
            { name: "Oranges", count: 12 },
            { name: "Lemons", count: 15 },
            { name: "Pears", count: 20 },
            { name: "Pineapples", count: 3 }
        ];
    });
NOTE
The dx-item-alias directive specifies the variable that is used to access the item object.
Knockout
HTML
JavaScript
<div data-bind="dxList: {
    dataSource: fruits,
    itemTemplate: 'listItem'
}">
    <div data-options="dxTemplate: { name: 'listItem' }">
        <b data-bind="text: name"></b><br />
        <p data-bind="text: count" style="margin:0px"></p>
    </div>
</div>
const viewModel = {
    fruits: [
        { name: "Apples", count: 10 },
        { name: "Oranges", count: 12 },
        { name: "Lemons", count: 15 },
        { name: "Pears", count: 20 },
        { name: "Pineapples", count: 3 }
    ]
};

ko.applyBindings(viewModel);

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

JavaScript
const fruits = [
    { name: "Apples", count: 10 },
    { name: "Oranges", count: 12 },
    { name: "Lemons", count: 15 },
    { name: "Pears", count: 20 },
    { name: "Pineapples", count: 3 }
];

$(function() {
    $("#listContainer").dxList({
        dataSource: fruits,
        itemTemplate: function(data, _, element) {
            element.append(
                $("<b>").text(data.fruit), $("<br />"),
                $("<p>").text(data.count).css("margin", 0)
            )
        }
    });
});

You can also customize an individual List 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>
const fruits = [
    { name: "Apples", count: 10 },
    { name: "Oranges", count: 12, template: $("#individualTemplate") },
    // ...
];

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.

View Built-In Template Engine Demo View 3rd-Party Template Engine Demo

See Also