Groups | Blog | Home
all groups > flash actionscript > march 2006 >

flash actionscript : preload value into combobox


p7m8
3/7/2006 9:17:44 PM
I have a combobox "stateCombo" and it has all the states loaded into it as:
Texsas, TX, Florida, FL etc...

populate_combo = function(){
var l = new LoadVars();
l.onLoad = function(){
var i;
var states = this.states.split(",");
var stateABVs = this.stateABVs.split(",");
var l = states.length;
for(i = 0;i < l;i++){
stateCombo.addItem(states,stateABVs);
}
}
l.load("stateCountryList.php");
}
populate_combo();


How do i have the state preselected when i load the clients data..so if the
client has "FL" as the his state of record; have "FLORIDA" selected in the
combobox. I currently have the lable as the state name and the data as the
state abv.

It should be easy, load combo box and show passed in state...HELP! i need to
set the combobox stateCombo.lable equal to the clients state that is passed in

Thank
R
has981
3/8/2006 12:00:00 AM
Hi there p7m8,

if you can get the index of the state you want to set in the combo box then you can use: my_cb.selectedIndex = state_index;

silverSurfer
3/8/2006 12:00:00 AM
The basic idea is this:
You need to create a listener for the comboBox
then set the required index
and then trigger it

You can either create an object, and assign a "change" handler for it,
or use Delegate.
Check the code.
Hope this helps
jr



// the object that handles the change
var myListener:Object = new Object();
myListener.change = function () {
// your combobox change code here
trace("combobox changed");

}
// now assign the listener to the combobox on stage
cbComboBox.addEventListener("change",myListener)

// now make an initial selection
cbComboBox.selectedIndex = 3 // a hardcoded value, or pass a variable here
cbComboBox.dispatchEvent({type:"change"})

// to try this, make a combobox called 'cbComboBox'
// hardcode some values into it
// test the movie

//------------------------------- //
// another way to do this is to use the Delegate class like this:
// this is a Class file that should be called 'TestSelection.as'
// to use it, wrap your components into a movieclip.
// (here I've used a listbox and a textfield)
// place that movieclip on stage and use the Library to add the text
'TestSelection' in the AS2 Class
// after clicking "export for actionscript"

// you can set the index you want to pass in the main timeline of your movie
like this
// (lets presume that your movieclip's instancename is 'myform_mc')
// this.myForm.theIndex= 5

// the class file/
// ------------------------------------ //
// ------------------------------------ //
import mx.utils.Delegate;
// ------------------------------------ //
class TestSelection extends MovieClip {
var me:MovieClip // for localisation / synchronicity
var lbTest:mx.controls.List; // the actual list
var meList:mx.controls.List; // the proxy list
var meDisplayText:TextField; // the feedback textfield

var theIndex:Number; // passed in from the parent clip
// ------------------------------------ //
// constructor
// ------------------------------------ //
function TestSelection () {
me = this;
meList = me.lbTest
meDisplayText = me.theValue; // the targeted text field
me.onLoad = function () {
trace ("TestSelection Class loaded");
meList.addEventListener ("change", Delegate.create (me, delegatedEvent));
meList.selectedIndex = theIndex;
delegatedEvent (); // call the initial selection
};
}
// ------------------------------------ //
// the function that gets called when the listbox is clicked
// ------------------------------------ //
function delegatedEvent () {
//var meDisplayText = me.theValue; // the targeted text field
meDisplayText.text = meList.selectedItem.data; // or .label etc
trace ("index : " + theIndex);
trace ("data : " + meList.selectedItem.data);
trace ("label : " + meList.selectedItem.label);
}
}
// ------------------------------------ //
p7m8
3/8/2006 1:07:00 AM
Im trying to do this but with states and not months....

<?php
// initialize or capture variable
$month = !isset($_POST)? NULL : $_POST;
?>
<select name="month">
<option value="<?php echo $month;?>" SELECTED><?php echo $month;?></option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<input type="submit" name="submit" value="Try Me!">
</form>

Dont tell me Flash cant populate a combobox with a value passed to it. Im new
to Flash but there has to be away.....

Thanks
R
p7m8
3/10/2006 6:24:57 PM
I got this idea from another site .... IT WORKS, it will load the combobox and
have an item preselected!

silverSurfer thanks for your help! When I tried the dynamic loading of the
states I got an undifined as the Index, when i added items through an array the
index worked fine?

cbComboBox.addEventListener("change",myListener)
cbComboBox.selectedIndex = 3
trace(cbComboBox.selectedIndex);// on trace it was undifined so i had to go
another route
cbComboBox.dispatchEvent({type:"change"})

________________________________________________________________________



var checkState = "TX";
ExpenseCodesV = new LoadVars()
ExpenseCodesV.load("http://localhost/identashield/stateCountryList.php");
ExpenseCodesV.onLoad = function(succes)
{
if(succes)
{
var states = this.states.split(",");
var stateABVs = this.stateABVs.split(",");

count = states.length;
for (i=0; i<count -1; i++)
{
if (stateABVs[i] == checkState)
{
statesCombo.addItem(states[i],stateABVs[i]);
var currentVal = i;
statesCombo.selectedIndex = currentVal;
}
else
{
statesCombo.addItem(states[i],stateABVs[i]);
}
}
}
else trace ("Error loading data")
}
AddThis Social Bookmark Button