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.
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
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