911 days ago
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.