View Full Version : [AS2] List component - setting initial selection

10-08-2008, 05:42 AM
I had been trying to figure out how to make an automatic selection of an item in a List component and I figured it out after messing with it for some time. This script makes the desired item in the list selected as soon as it loads. If listeners are associated with the component instance, 'itemRollOver' and 'change' events are also triggered if available.

I thought someone else might need it so here it is.

myList.doLater(this, "selectInitialItem");
// change 'myList' above to the List instance of yours

var initIndex:Number = 0;
// initial index of the List instance to be activated

function selectInitialItem() {
var list:mx.controls.List = myList;
// change 'myList' above to the List instance of yours
var s:Number = initIndex;
if (initIndex == undefined || initIndex < 0) s = 0;
else if (initIndex > list.dataProvider.length-1) {
s = list.dataProvider.length - 1;
list.selected = {};
list.selected[s.toString()] = s;

10-08-2008, 08:29 PM
I just figured out something. The 'selectedIndex' property sets the item at index 'selectedIndex' selected, but it doesn't trigger the 'itemRollOver' or 'change' events. So the difference between assigning it a number and using the script above is basically this.

03-21-2009, 03:58 PM
Nice, info on the AS2 components are always good to have..

I'll add this to my list of Components: tips and tricks