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.

2 Comments »

  1. Maxwell Said,

    August 8, 2009 @ 9:23 pm

    Good stuff. There’s also the filterFunction that you can assign to an ArrayCollection that will let you manipulate which elements in the array source the AC will expose to bound components. I used that to implement paging in a TileList on one project.

  2. slaingod Said,

    January 7, 2010 @ 11:28 am

    You must learn the ways of the Flex Monkey Patch, young Padawan. :) Nothing like myTileList::mx_internal.rendererArray, etc. to save the day as well. Rip that class wide open.

RSS feed for comments on this post

Leave a Comment