PDA

View Full Version : Having trouble selecting a combo box item


msimmons
01-18-2006, 05:53 PM
I am loading a mc that has a combo box on it and I want to pre select an item... this combo is built from the database via xml. an example of the data would be:

index : data : label
0 : 123456 : john doe
1 : 123457 : jane doe
2 : 123458 : someone else

and so on...

so I have the "123456" part and I want to pre select it... how?
thanks :)

flashead
01-18-2006, 05:55 PM
instanceNameOfComboBox.setSelectedIndex(number);
And pass a number, 0 being the first item in your combo, 1 being the 2nd etc.

msimmons
01-18-2006, 05:59 PM
i could have done that but...
I do not know what number 612617 corresponds to. The combo is populated from a database so I do not know how many records or at what position they will be at design time.

flashead
01-19-2006, 05:28 AM
oh.
now that i've read your question a second time, i think i understand what you're asking.
what you need is a little function to loop through whatever array/object is set as the dataProvider for your combobox. and search for data that matches n:
// an array populated by an xml file. (populated by me for this example)
var dataArray = [
{ label:"Harry", data:123456 },
{ label:"Sally", data:123457 },
{ label:"Fred", data:123458 },
{ label:"Roxanne", data:123459 }
];
//
comboBox.dataProvider = dataArray;
//
// the data we want to search for.
var userDefinedVariable = 123459;
//
// set the selectedIndex to whatever the queryData function returns.
comboBox.selectedIndex = queryData( userDefinedVariable );
//
function queryData( query )
{
trace( "searching for:" + query + newline );
//
// loop through the array that provides the data to the combobox.
var l = dataArray.length;
for ( var i=0; i<l; i++ )
{
var trc = "[" + i + "] : " + dataArray[i].data;
// test for equality between the query parameter passed and the value of data for the current item.
// if the two match, return the value of i. which, in this example, will equal 3 which is the index of 123459.
if ( dataArray[i].data == query ) return i;
else trc += " > item not found.";
//
trace( trc );
}
// if the query value isn't found, default to something else ( in this case the first item).
return 0;
}

---
i should mention that the method i suggested in my earlier post is the component v1 way. if you're using v2 it would be:
instanceNameOfComboBox.selectedIndex = 2;
all code in my example up top is the v2 method.

msimmons
01-19-2006, 03:37 PM
Cool, thanks for that :) since we're in a time crunch we just redesigned the way we have the program to function so I won't be needing it this time but being such an obvious feature (one of many that flash makes difficult to do) I'm sure I'll need it again before too long so I've marked this post in favorites and will be sure to let you know how it works out.
Thanks again!
(oh and as far as v1 vs v2 go, I assume we're using v2 (we're using the latest version of flash)... I don't do any of the design, just the code behind it so not sure
M

msimmons
03-22-2006, 04:05 PM
Its been just a little over two months, seems like so long ago :)
Here is what I did, It is pretty much what you are saying, but in a reuseable function:

//**FUNCTION FOR SELECTING CBO INDEX BASED ON DATA VALUE
function selectCboIndexByData(theCbo,theDataValue):Number{
//loop through ever item in the cbo
for(var i:Number=0; i<theCbo.length; i++){
//check to see if we have found what we are looking for
if(theCbo.getItemAt(i).data == theDataValue){
//when we find what we want pass it back to the caller
return i;
}
}
//if we make it all the way to the end without finding what we want just return 0
return 0;
}

now if I want to display something in any of my cbo's I just do this:

myCbo.selectedIndex = selectCboIndexByData(myCbo, '612617');

shauns1
04-24-2008, 07:35 AM
Its been just a little over two months, seems like so long ago :)
Here is what I did, It is pretty much what you are saying, but in a reuseable function:

//**FUNCTION FOR SELECTING CBO INDEX BASED ON DATA VALUE
function selectCboIndexByData(theCbo,theDataValue):Number{
//loop through ever item in the cbo
for(var i:Number=0; i<theCbo.length; i++){
//check to see if we have found what we are looking for
if(theCbo.getItemAt(i).data == theDataValue){
//when we find what we want pass it back to the caller
return i;
}
}
//if we make it all the way to the end without finding what we want just return 0
return 0;
}

now if I want to display something in any of my cbo's I just do this:

myCbo.selectedIndex = selectCboIndexByData(myCbo, '612617');



Just wanted to say thanks for this handy function.