Link to home
Start Free TrialLog in
Avatar of stargateatlantis
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>

Open in new window

Avatar of Gary Benade
Gary Benade
Flag of South Africa image


public function countryinit():void
{
    lstcountry.selectedItem = findCountry('CAD');
}  
public function findCountry( countryCode:String):Object
{
    for each( var c:Object in sercountry.lastResult.countires.ctnry)
    {
        if( c.@countryCode == countryCode)
            return c;
    }
    return null;
}
 
 
 
here is my working test app
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
	<mx:Script>
		<![CDATA[
			[Bindable] private var xml:XML = <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>
			private function init( countryCode:String):Object
			{
				for each( var c:Object in xml.ctnry)
    			{
        			if( c.@countryCode == countryCode)
            			return c;
			    }
    			return null;
			}
		]]>
	</mx:Script>
	<mx:ComboBox x="21" y="54" width="143" id="lstcountry" labelField="@label" dataProvider="{xml.ctnry}" creationComplete="lstcountry.selectedItem=init('CAN');"></mx:ComboBox>
</mx:Application>

Open in new window

Avatar of stargateatlantis
stargateatlantis

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>

Open in new window

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>

Open in new window

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>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gary Benade
Gary Benade
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial