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.

JavaScript
  • const fruits = [
  • { text: "Apples", badge: 10 },
  • { text: "Oranges", badge: 12, disabled: true },
  • { text: "Lemons", badge: 15, visible: false }
  • ];
  •  
  • $(function() {
  • $("#listContainer").dxList({
  • dataSource: fruits
  • });
  • });

If you need a more flexible solution, define an itemTemplate.

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") },
  • // ...
  • ];

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

See Also