Click here to Skip to main content
Click here to Skip to main content

WinJS, ListViews and Limiting the Number of Items

By , 28 Feb 2013
 

When working with a Listview in Windows 8 applications using HTML and JavaScript, one of the challenges that is often faced is how to limit the number of items added to ListView control.  There are different reasons to limit the number of items, but the one I am going to focus is where you want to fill the vertical space of the ListView (using the ListLayout render function) with as many items as possible but without causing the ListView to scroll vertically.

Image demonstrating insufficient padding for an embedded pannable area.This is a common ‘problem’ when building modern UI applications.  Because Windows 8 apps typically pan horizontally, the design guidelines state that you should not have vertically scrollable content.  Looking at the picture to the right, imagine that those "vertical" regions are displaying tweets from various users you are following.  You want to show as many tweets as possible for each user, but you do not want to show so many as to have the ListView enable scrolling.

So solve this problem, use the createFiltered function on the Binding List you are using to put data into the ListView.  For example, I may have setup my ListView of tweets like this in my HTML:

<div id="tweets" data-win-control="WinJS.UI.ListView" 
     data-win-options="{itemTemplate:select('#tweetTemplate'),
                        layout:{type:WinJS.UI.ListLayout},
                        selectionMode:'none',
                        swipeBehavior:'none'}">
</div>

In my page.js file, I need to do a few things.  First, I need to get the height of my ListView.  So after my page has loaded (typically in the ready handler for my Page control), I do this:

var tweetList = document.getElementById('tweets');
var listHeight = tweetList.clientHeight;

with that, I can now setup my Binding List like this:

function displayTweets (tweetArray, listHeight) {
     var count = 0;
     var list = new WinJS.Binding.List(newTweets).createFiltered(
           function (item) {
                 count += 90;
                  return (count < listHeight
           }
     );
     tweets.winControl.itemDataSource = list.dataSource;
}

Now you will always get the correct number of items displayed in your ListView regardless of the screen size.

This blog post was originally posted here: http://www.slickthought.net/post/2013/02/06/WinJS-ListViews-and-Limiting-the-Number-of-Items.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jeff Brand
Web Developer
United States United States
Member
I've been with Microsoft for over 10 years now. I started with the company soon after the release of Windows 95. Now those were heady days. I've seen the long march of Windows 2000 and was right there for the browser wars. While at Microsoft, I have been a consultant, an ecommerce specialist, and an enterprise technology advisor. Prior to Microsoft, I was in the Air Force stationed at Offutt AFB in Omaha, NE where I was Chief Network Engineer (my pre-developer days) for the USSTRATCOM LAN.
 
Today, I am a .NET Developer Evangelist for Microsoft's North Central District. I live in Minneapolis and often travel to the surrounding states of Nebraska, Iowa, and the Dakotas (ok, not so much the Dakotas). In my current role, I am responsible for working with developers and customers that are evaluating or using .NET, especially Windows 8 and Windows Phone 8.
 
My hobbies and interests include: football, basketball, golf, poker, reading, painting miniatures, and Michigan State sports!
 
Check out my blog at http://slickthought.net, follow me on Twitter @jabrand, and make sure you check out http://aka.ms/30Days

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 28 Feb 2013
Article Copyright 2013 by Jeff Brand
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid