stargateatlantis
asked on
mx:ComboBox selected item on load
Basically i have a XML file called countries.xml which contains the following XML code. What I want to do is when the application runs CANADA is selected in the combo box.
<countires>
<ctnry countryCode='0' label='Select Country' />
<ctnry countryCode='CAN' label='Canada' />
<ctnry countryCode='USA' label='United States' />
<ctnry countryCode='UK' label='United Kingdom' />
<ctnry countryCode='OTH' label='Other' />
</countires>
FLEX CODE BELOW
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initDash();">
<mx:Script>
<![CDATA[
var lstCounVal;
//int dash when load
public function initDash():void
{
this.sercountry.send();
}
//Close Event Country//
public function handleCloseEventCountry(eventObj:Event):void
{
//Get Country value//
lstCounVal=lstcountry.selectedItem.countryCode;
}
public function countryinit():void
{
lstcountry.selectedItem = "{countires['CAD']}";
}
]]>
</mx:Script>
<mx:ComboBox x="21" y="54" width="143" id="lstcountry" labelField="label" dataProvider="{sercountry.lastResult.countires.ctnry}" creationComplete="countryinit();"></mx:ComboBox>
<mx:HTTPService id='sercountry' url='countires.xml' useProxy="false" method="POST">
</mx:HTTPService>
</mx:Application>
ASKER
Didn't work here is my modified code.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initDash();">
<mx:Script>
<![CDATA[
var lstCounVal;
//int dash when load
public function initDash():void
{
this.sercountry.send();
lstcountry.selectedItem = findCountry('CAD');
}
//Find Country in list and select it//
public function findCountry( countryCode:String):Object
{
for each( var c:Object in sercountry.lastResult.countires.ctnry)
{
if( c.@countryCode == countryCode)
return c;
}
return null;
}
//Close Event Country//
public function handleCloseEventCountry(eventObj:Event):void
{
//Get Country value//
lstCounVal=lstcountry.selectedItem.countryCode;
}
]]>
</mx:Script>
<mx:ComboBox x="21" y="54" width="143" id="lstcountry" labelField="label" dataProvider="{sercountry.lastResult.countires.ctnry}" creationComplete="lstcountry.selectedItem=findCountry('CAN');"></mx:ComboBox>
<mx:HTTPService id='sercountry' url='countires.xml' useProxy="false" method="POST">
</mx:HTTPService>
</mx:Application>
You're calling findcountry before the async service has returned data
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initDash();">
<mx:Script>
<![CDATA[
var lstCounVal;
//int dash when load
public function initDash():void
{
this.sercountry.send();
}
private function result( e:ResultEvent):void
{
lstcountry.selectedItem = findCountry('CAD');
}
//Find Country in list and select it//
public function findCountry( countryCode:String):Object
{
for each( var c:Object in sercountry.lastResult.countires.ctnry)
{
if( c.@countryCode == countryCode)
return c;
}
return null;
}
//Close Event Country//
public function handleCloseEventCountry(eventObj:Event):void
{
//Get Country value//
lstCounVal=lstcountry.selectedItem.countryCode;
}
]]>
</mx:Script>
<mx:ComboBox x="21" y="54" width="143" id="lstcountry" labelField="label" dataProvider="{sercountry.lastResult.countires.ctnry}" creationComplete="lstcountry.selectedItem=findCountry('CAN');"></mx:ComboBox>
<mx:HTTPService id='sercountry' url='countires.xml' useProxy="false" method="POST" result="result()">
</mx:HTTPService>
</mx:Application>
ASKER
It still doesn't work take a look at your code.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initDash();">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
var lstCounVal;
//int dash when load
public function initDash():void
{
this.sercountry.send();
}
private function result(e:ResultEvent):void
{
lstcountry.selectedItem = findCountry('CAD');
}
//Find Country in list and select it//
public function findCountry( countryCode:String):Object
{
for each( var c:Object in sercountry.lastResult.countires.ctnry)
{
if( c.@countryCode == countryCode)
return c;
}
return null;
}
//Close Event Country//
public function handleCloseEventCountry(eventObj:Event):void
{
//Get Country value//
lstCounVal=lstcountry.selectedItem.countryCode;
}
]]>
</mx:Script>
<mx:ComboBox x="21" y="54" width="143" id="lstcountry" labelField="label" dataProvider="{sercountry.lastResult.countires.ctnry}" creationComplete="lstcountry.selectedItem=findCountry('CAN');"></mx:ComboBox>
<mx:HTTPService id='sercountry' url='countires.xml' useProxy="false" method="POST" result="result(event)">
</mx:HTTPService>
</mx:Application>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window