Using the TileList Control

I'm experimenting using the mx:TileList control in Flex 2.0. This control is likely to be one of the primary components in displaying information about products. My primary references for this tutorial are the Flex 2 Developer's Guide (the TileList control is under the section on Using Data-Driven Controls) and the Flex 2 Language Reference section on class TileList.

The Flex 2 Developer's Guide just has a brief section on how to use the TileList control. For such a powerful and key component, the write up and examples are quite skimpy. The TileList control "displays a tiled list of items. The items are tiled in vertical columns or horizontal rows." The TileList control uses an item renderer to determine how to display the item. There is a default item renderer used by TileList, but the default item renderer can display only text and an icon (see the example in the Flex 2 Language Reference). Most of the time you will want to create a custom item renderer to control how each item is displayed in the TileList. Each item in the TileList occupies the same width and height.

In my example of using the TileList control, I am displaying a list of book items. Each book item occupies a single row and the list scrolls vertically. Per the developer's guide "The TileList control lays out its children in one or more vertical columns or horizontal rows, starting new rows or columns as necessary." I intentionally set the width of my TileList so that only one book item would occupy a row. The power of the TileList is that it can accommodate an unknown number of items. In my example, if you select a different category, the TileList control resizes as necessary.

The width, height, rowHeight and columnWidth attributes are key to determining how many items are displayed horizontally and vertically. I had to play with this several times before I got the look I wanted.

<mx:TileList dataProvider="{bookAryCol}" height="470" width="400" itemRenderer="ThumbNail"
   rowHeight="150" columnWidth="380" id="bookList" borderStyle="solid" borderColor="#ff8040"
   borderThickness="3" rollOverColor="#ff8040" selectionColor="#ff8010"
   />

A TileList needs a data provider to determine what to display. In my example, I am using ColdFusion to provide an array of Book objects. Flex sends the category selected to a ColdFusion CFC method that uses the category to get from amazon.com up to 10 books. CF then creates a Book object for each book found, adds the Book object to an array, and returns the array of Book objects to Flex. This technique is similar to my tutorial on exchanging an array of objects between CF and Flex.

When Flex receives the array of Book objects, it converts the array into an ArrayCollection (view source on the example and see function handleBookResult). The ArrayCollection object is the data provider for the TileList control. Note that I have an ActionScript Book class that is related to my CF Book class.

The TileList control uses a custom item renderer to determine what information to display for each Book object and how to display that data. My custom item renderer is named ThumbNail.mxml. The ThumbNail component is a horizontal box which displays the book's cover image, the title, and price. In the ThumbNail.mxml file I refer to the object's fields that provide the actual values with the expression {data.fieldName} for example {data.title} for the title. The field names match the instance variable names in my Book class.

<?xml version="1.0"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="380" height="200" verticalAlign="middle" verticalGap="0"
verticalScrollPolicy="off">


   <mx:CurrencyFormatter id="cf"/>
   
   <mx:Image id="img" height="100" width="80" source="{data.image}"/>
   
   <mx:VBox width="80%" paddingTop="0" horizontalGap="4">
   
      <mx:Text width="90%" text="{data.title}" fontWeight="bold" textAlign="left"/>
      
      <mx:Label text="{cf.format(data.price)}" fontWeight="bold"/>
      
   </mx:VBox>

</mx:HBox>

In future examples of using the TileList control I'll explore how a user can interact with the items being displayed.

Comments
hi bruce, great tutorial,
i am trying to dynamically add images to my TileList, but they appear as text, i am a little confused by how TileList works. Is it not as simple as doing this...

   var img:Image = new Image();
   img.load( 'cow.png' );

   var a:Array = [ img ];
   myClips = new ArrayCollection( a );

   myclipsTL = new TileList();
   myclipsTL.dataProvider = myClips;
# Posted By brett hartshorn | 12/9/06 11:26 AM
Consult the link I provide in the blog entry to the Flex Language reference TileList class. There is an example there that includes displaying a label and an icon in the TileList.

In my demo app for this blog entry, I used a custom component to get an image to display in the TileList component. The custom component included an image component and the source property was bound to the image field of my object. Using a custom component gives you that ability to display more information in the TileList.
# Posted By Bruce | 12/9/06 1:45 PM
Brett:

Also I think the Object that is in your ArrayCollection must have a label property and an icon property. The icon property is the image to display.

In your code sample, you have the image object as the object in the ArrayCollection and that will not work for a TileList.

Try creating an ArrayCollection of Objects that have a label property and an incon property (see the example here: http://livedocs.macromedia.com/flex/2/langref/mx/c...
# Posted By Bruce | 12/9/06 1:49 PM
hey i have a tilelsit all setup with a data provider im using xml to populate the tilelist with image. now how do I get the same result as you with a slider control and radio buttons.

the slider is for filtering price and the radio buttons are for options.

please help ive been stuck for ages:(
# Posted By Ben | 3/16/07 6:15 PM
Ben:

I can only recommend you read through my articles on using the tilelist, study the references I provide in the articles, and study my code examples.

My job is crazy right now and I don't have the extra time to trouble shoot other projects.
# Posted By Bruce | 3/16/07 6:37 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.