• .NET Developer Tools DevTools

    UI controls for ASP.NET AJAX, MVC, WPF,
    Silverlight, Windows 8 and Windows Phone

  • Hybrid Mobile Development Icenium

    Cross-platform Mobile Development Tool
    with cloud-based architecture

  • HTML5 / JavaScript Development Kendo UI

    Everything you need to build sites and
    mobile apps with JavaScript and HTML5

  • Testing Tools TestStudio

    One easy tool for Functional, Performance,
    Load and Mobile software testing

  • Web Presence Platform Sitefinity CMS

    Everything for your online business - content
    management, ecommerce, emarketing

  • Agile Project Management TeamPulse

    Simple and intuitive project management
    and collaboration software

Contact us

We are here for you.
  • usa+1‒888‒365‒2779
  • uk+44‒20‒7291‒0580
  • bg+359‒2‒8099850
  • de+49‒89‒2441642‒70
  • au+61‒2‒8090‒1465
  • emailsales@telerik.com
Your account Access to your products, updates and support
Telerik Product Families
  • Your Account
    Your Account
    Log in
  • ABOUT US

    About Telerik

    • Company
    • Press Center
    • Customers
    • Community
    • Careers
    • Contacts
Kendo UI - The way of HTML5
Products ▼
Kendo UI Web Kendo UI Mobile Kendo UI DataViz Server Side Wrappers
Demos Purchase Download
Blogs Documentation
Support ▼
Premium Forums StackOverflow Forums
Resources ▼

Featured Resource

Kendo UI Dojo


Blogs Code Library Demos Documentation FAQ Testing
Premium Forums Roadmap User Voice Videos Webinars More Resources
Contact Us Search
 

Blogs

An In-Depth Look at the ListView

Thursday, April 12, 2012 by Kendo UI Team Blog | Comments 7

The ListView is a widget that was recently introduced in the March 2012 release of Kendo UI Web. Its purpose is to display a custom layout of data-bound items through templates. In this article, I'll highlight how the ListView works as well as identify scenarios where it is suitable to use.

The ListView is ideally suited for scenarios where you wish to display a list of items in a consistent manner. Examples of its use can be seen in commonplace design structures applied on the Internet today; search engine results, tweets from Twitter, Facebook updates, inbox items in Gmail, card lists in Trello, and so on.

The ListView is designed to put you back in control when it comes to displaying data. In fact, it does not provide a default rendering of data-bound items. Instead, it relies entirely on templates to define how a list of items - including alternating items and items being edited - is displayed.

Let's see how the ListView works by building a simple example that integrates the Twitter API.

First, we'll define a target HTML element such as a list or div:

Target Element and Template for Rendering ListView Items

<div id="listView"></div>

<script type="text/x-kendo-tmpl" id="template">
    <div class="tweet">
        <img class="profileImage" src="${profile_image_url}" />
        <strong>${from_user_name}</strong>
        <a href="http://twitter.com/${from_user}">@${from_user}</a>
        <p class="text">${text}</p>
    </div>
</script>

Next, we'll initialize the ListView by referring the template and a result set from the Twitter API to be displayed:

ListView Initialization with Remote DataSource (Twitter)

var dataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: "http://search.twitter.com/search.json",
            contentType: "application/json; charset=utf-8",
            type: "GET",
            dataType: "jsonp",
            data: {
                q: "#KendoUI"
            }
        }
    },
    schema: {
        data: "results",
        total: "results_per_page"
    }
});

$("#listView").kendoListView({
    dataSource: dataSource,
    template: kendo.template($("#template").html())
});

Here's the live example of the representation (above) with additional styling:

In scenarios where the number of items bound to a ListView is larger than expected, a Pager will control the items being displayed. Using a Pager is relatively simple. First, you create a target element for its rendering. Typically, it should be placed in the vicinity of the ListView.

Target Elements for ListView and Pager with Template for Rendering ListView Items

<div id="listView"></div>
<div id="pager"></div>

<script type="text/x-kendo-tmpl" id="template">
    <div class="tweet">
        <img class="profileImage" src="${profile_image_url}" />
        <strong>${from_user_name}</strong>
        <a href="http://twitter.com/${from_user}">@${from_user}</a>
        <p class="text">${text}</p>
    </div>
</script>

The next step is to update the ListView configuration to state that it support paging through its pageable property and to initialize the Pager.

ListView and Pager Initialization with Remote DataSource (Twitter)

var dataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: "http://search.twitter.com/search.json",
            contentType: "application/json; charset=utf-8",
            type: "GET",
            dataType: "jsonp",
            data: {
                q: "#KendoUI"
            }
        }
    },
    schema: {
        data: "results",
        total: "results_per_page"
    }
});

$("pager").kendoPager({
	dataSource: dataSource
});

$("#listView").kendoListView({
    dataSource: dataSource,
    pageable: true,
    template: kendo.template($("#template").html())
});

Here's the same live example with a Pager applied to the ListView:

From a design perspective, it may be useful to visually differiante each alternating item in a ListView. For example, in the previous example, I may wish to have every second item have a slightly darker background (i.e. banded rows). Defining the altTemplate property accomplishes this through the use of a template that you define. Let's go ahead and update our working example to include a template for alternating items.

Target Elements with Template and Alternating Template for Rendering ListView Items

<div id="listView"></div>
<div id="pager"></div>

<script type="text/x-kendo-tmpl" id="template">
    <div class="tweet">
        <img class="profileImage" src="${profile_image_url}" />
        <strong>${from_user_name}</strong>
        <a href="http://twitter.com/${from_user}">@${from_user}</a>
        <p class="text">${text}</p>
    </div>
</script>
<script type="text/x-kendo-tmpl" id="altTemplate">
    <div class="altTweet">
        <img class="profileImage" src="${profile_image_url}" />
        <strong>${from_user_name}</strong>
        <a href="http://twitter.com/${from_user}">@${from_user}</a>
        <p class="text">${text}</p>
    </div>
</script>

Here's how the live example how looks with a template for alternating items:

In addition to paging, the ListView supports item selection, navigation, and inline editing. Supporting these operations is achieved through the initialization of its Boolean configuration options. In the case of inline editing, the ListView provides the editTemplate property, which defines a template for this mode. Once define, the ListView can render out this editing template via the edit method. When invoked, the editTemplate for the ListView is applied against the target item. In most scenarios, you should implement this through an event model that is triggered when the user selected an item to modify.

ListView Initialization with an Edit Template

var listView = $("#listView").kendoListView({

    template: kendo.template($("#template").html()),
    editTemplate: kendo.template($("#editTemplate").html()),
    altTemplate: kendo.template($("#altTemplate").html())

});

The ListView encapsulates operations for adding and removing items, item selection, editing, and persisting changes. These methods enable you to modify the underpinning list of items through a series of user-initiated actions/events. In the case of inline editing, the first step is to define a template that is to be used when editing items.

Enabling ListView Paging, Selection, Navigation and Editing

$(document).ready(function(){
    $("#listView").kendoListView({

        pageable: true,
        selectable: true,
        navigatable: true,
        editable: true

    });
});

The template you define for the inline editing of items may include other Kendo UI widgets. Looking at the example for editing items on KendoUI.com, you can see that the edit template defines a series of widgets for editing an item:

The inline editing of ListView items is triggered by a click event initiated by a user and is wired up via .delegate() in jQuery.

ListView Event Handling for User Interactions

var listView = $("#listView").kendoListView({
    dataSource: dataSource,
    template: kendo.template($("#template").html()),
    editTemplate: kendo.template($("#editTemplate").html())
}).delegate(".k-edit-button", "click", function(e) {
    listView.edit($(this).closest(".product-view"));
    e.preventDefault();
}).delegate(".k-delete-button", "click", function(e) {
    listView.remove($(this).closest(".product-view"));
    e.preventDefault();
}).delegate(".k-update-button", "click", function(e) {
    listView.save();
    e.preventDefault();
}).delegate(".k-cancel-button", "click", function(e) {
    listView.cancel();
    e.preventDefault();
}).data("kendoListView");

$(".k-add-button").click(function(e) {
    listView.add();
    e.preventDefault();
});

Item selection is another scenario supported by the ListView. By setting the selectable property to either "single" or "multiple", you can provide the ability to have users select items.

Initialization of a Selectable ListView

$("#listView").kendoListView({
    dataSource: dataSource,
    pageable: true,
    selectable: "multiple",
    template: kendo.template($("#template").html())
});​

You can capture when items are selected through the change event that is triggered when a user selected one or more items (via shift-select).

Wiring Up the ListView Change Event

$("#listView").kendoListView({
    change: function(e) {
        var data = dataSource.view(),
            selected = $.map(this.select(), function(item) {
                return data[$(item).index()].ProductName;
        });

        // index selected or read item information through data
     }
 });

Updating the example using the Twitter API that I created before, here's the live representation of that with item selection enabled:

As you've seen, the ListView is ideally suited for scenarios where you wish to display a list of items in a consistent manner. I hope this blog post has shed some light on the use of this widget. As always, if you have any questions, please feel free to drop us a line on Twitter (@KendoUI).

About the Author
John Bristowe (@JohnBristowe) is a Developer Evangelist with Telerik and is based out of Melbourne, Australia. John enjoys all the meats of our technology stew, hacking on everything from angle brackets to .NET. John's passion lies in modern web standards like HTML5 and CSS3. His website is bristowe.com.

7 Comments

  1. 1 Clix 18 Apr 2012
    Is it possible to have a listview where each item field is editable per se? For instance you have a Title and a Description, and want to have separate edits in place for each field. Is this possible? And, can we have that with MVVM?
  2. 2 Ambica 16 May 2012
    Hi Jhon,

    How to refresh kendo pager ?
     
  3. 3 Jeanelle 28 Jun 2012
    Great article.  Is it possible to use that add method of the list view to add data from a form on the page instead of adding an empty item to the listview?
  4. 4 Jayesh Goyani 19 Oct 2012
    nice post
  5. 5 Drophit 18 Jan 2013
    Does this work with kendoMobileListview?
  6. 6 Romady Minaya 03 Jan 2013

    Hi John,

    It is possible to change the look of the pagination links as same with the lists?

  7. 7 John 09 Apr 2013
    What is the best way to hide the pager? e.g. if my pageSize = 10 but I only have zero or 1 record to display, I do not want to show the pager.

Comment

  1. Click to add

  2. Click to add

  3. Click to add

  4.    
     
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
     
      
       
Blogs feed
Categories

  • Tutorials (26)
  • Release (33)
  • Browsers (7)
  • Extensions (3)
  • Tip of the Week (10)
  • Videos (5)
  • Concepts and Theory (13)
  • Misc. (25)
  • Framework Constructs (6)
  • Mobile (6)
  • UI Widgets (5)
  • Blogs (1)
Archive
  • 2013 May (6)
  • 2013 April (10)
  • 2013 March (9)
  • 2013 February (12)
  • 2013 January (10)
  • 2012 December (9)
  • 2012 November (11)
  • 2012 October (6)
  • 2012 September (7)
  • 2012 August (8)
  • 2012 July (10)
  • 2012 June (8)
  • 2012 May (10)
  • 2012 April (7)
  • 2012 March (13)
  • 2012 February (10)
  • 2012 January (6)
  • 2011 December (10)
  • 2011 November (4)
  • 2011 October (6)
  • 2011 September (5)
  • 2011 August (9)
Home Web Mobile DataViz Server Wrappers Whitepapers Surveys Chrome Icenium Contact Us

Kendo UI framework is developed by Telerik - a leading provider of UI components for web, desktop and mobile applications. Trusted by over 100,000 customers worldwide for our devotion to quality and industry-best technical support, Telerik helps professionals maximize their productivity and "deliver more than expected" every day.

kendoui - powered by html5, css3 & jquery
get social
  • Twitter
  • Facebook
  • Google plus
  • RSS
Privacy Policy | Branding Guidelines
Powered by Sitefinity CMS

Copyright © 2011 - 2013 Telerik Inc. All rights reserved.