I'm a software developer living in Midtown, Atlanta. I wrote the MetaForum software in 2004. More recently I've been doing Ruby on Rails development for a startup named Vitrue. I also maintain the Forum Atlanta messageboard. Feel free to stop by and say hello!

Limiting a Flex TileList component’s number of selected items

If you want to set a maximum number of selected items in an ActionScript TileList component, there isn’t any straightforward way to do it built in.  Strangely.

First, catch the itemClick event in your tile list.

<mx:TileList id="foo" ... itemClick="checkMax(event)" />

Then in checkMax, the basic idea is to compare the current count of selected items;  if its too long, unselect the most recently selected item.  In this example I wanted to limit it to a max of 20 selected items.

            private function checkMax(e:Event):void {
                if (foo.selectedItems.length > 20) {
                    var x:int = foo.selectedIndices.indexOf(e.currentTarget.selectedIndex);
                    var tmpArray:ArrayCollection = new ArrayCollection(foo.selectedIndices);
                    tmpArray.removeItemAt(x);
                    foo.selectedIndices = tmpArray.toArray();
                }
            }

Since the Array class doesn’t have the removeItemAt method, we have to create a temporary ArrayCollection object first as a copy of the selectedIndices of our TileList.  Remove the most recently selected item, then save it back to the TileList.  Thanks Micah for helping to figure this out.