TileList Control Click and Drag-and-Drop

In a previous blog entry , I provided an introduction on using the TileList control. In this entry, I'll describe how a user may interact with the TileList. Specifically, I'll provide an example of the click event and how to enable drag-and-drop operations for the TileList.

In the Flex example for this blog entry I've added some components to the right of the original TileList. The first two components to the right are a Text and TextArea component. When the user clicks on a specific book in the TileList, these components are updated with that item's title and description. All I had to do was bind the text and htmlText values to the bookList (the id value for the TileList) selectedItem and then the name of the field (note that these fields must exist in my Book class).

<mx:Text text="{bookList.selectedItem.title}" fontSize="12" width="320" fontWeight="bold" />

<mx:TextArea htmlText="{bookList.selectedItem.description}" width="400" height="180" fontSize="10"
    editable="false" borderStyle="solid" borderColor="#ff8040"
borderThickness="3" id="bookDesc" />

The next user interaction feature I added was drag-and-drop. Chapter 29 in the Flex Developer's Guide provides a good overview on how to enable drag-and-drop operations. TileList (like the other List controls) includes built-in support for drag-and-drop. To enable the user to drag a book item from the TileList I just had to set dragEnabled="true". Note that I did not set dragMoveEnabled to true (false is the default) as I want the user to copy the item to the drop target, but still have the same item remain in the original TileList control.

I added a second TileList control to be the recipient of the drag-and-drop operation. To enable a TileList to be a "drop target" you just need to set dropEnabled="true". I am also using a different custom item renderer (see ThumNailBrief.mxml in the source) for this drop TileList. This item renderer just shows the title and price, so that is all that appears in the drop TileList.

<mx:TileList height="270" width="400" itemRenderer="ThumbNailBrief"
   rowHeight="60" columnWidth="380" id="cartList" borderStyle="solid" borderColor="#ff8040"
   borderThickness="3" rollOverColor="#FF8080" selectionColor="#FFFF00" dropEnabled="true"
   dragDrop="addToCart(event);" />

When the user drop's an item into the second TileList, a dropEvent is generated (there are many other drag-and-drop events, see chapter 29). When the dropEvent is triggered I call function addToCart(). This function merely creates a Book object using the ActionScript Book class, sets the value for some of the Book objects instance fields, and then adds the Book object to different ArrayCollection (cartAryCol).

When the user clicks on the check out button, I call a checkOut function. In the checkOut function the program loops through the cartAryCol and shows the user the items in the cartAryCol using an Alert component.

Of course, I'm missing several key functions. I need to add a way for the user to specify the quantities of a book he/she wants to order and I also need a way for the user to remove a book item from the cart TileList. I'll add those capabilities in the next iteration of my research on using the TileList control.

Check out the demo Flex example, right click to view its source. Feel free to provide any comments or links to other good examples of using the TileList control.

Related Blog Entries

Comments
Hey Bruce, great little demo. I actually just posted an item on my site along the same lines, customizing the dragImage used by the DataGrid.

http://www.dgrigg.com/post/DataGrid-Drag-Image

Keep up the good work.
# Posted By Derrick Grigg | 11/3/06 7:45 PM
Derrick - First off your blog rocks. I've learned alot.
Second check out: /links.cfm
# Posted By Bruce | 11/3/06 8:31 PM
Bruce, thank you a lot! Your example helped me to understood how to solve my problem with TileList. I've waisted some time trying to get the name of clicked TileList item via event handler, and your code just moved me from sticky point: I just should have to get the selectedItem's title!

Thank You!

BTW, your page is the #1 result of Google's 'TileList example' request :)
# Posted By Rostislav Siryk | 11/23/06 1:33 PM
Rostislav - glad my blog entry helped you solve a problem.
# Posted By Bruce | 11/23/06 4:51 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.