Saturday, March 20, 2010    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Nov 2006 >>
S M T W T F S
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

Search

Categories
 • Acrobat (3) [RSS]
 • Adobe (90) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (1) [RSS]
 • AIR (219) [RSS]
 • Appearances (191) [RSS]
 • Books (72) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1381) [RSS]
 • Data Services (34) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (197) [RSS]
 • Flex (498) [RSS]
 • Home Automation (5) [RSS]
 • Jobs (116) [RSS]
 • JRun (14) [RSS]
 • Labs (43) [RSS]
 • LiveCycle (34) [RSS]
 • MAX (232) [RSS]
 • Mobile (120) [RSS]
 • Regular Expressions (17) [RSS]
 • RIA (21) [RSS]
 • SQL (40) [RSS]
 • Stuff (536) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (162) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
November 22, 2006

Flex ComboBox To Display U.S. States And Canadian Provinces

Lots of us have needed a Flex ComboBox populated with U.S. States, and there are examples of that out there. But what I needed for an app I am working on is something a little different, a ComboBox that could display U.S. States or Canadian Provinces (but not at the same time, obviously). One simple (albeit ugly) solution would be to have two controls, switching the active one as needed. But nah, a better solution would be a control that does both. And so, with lots of invaluable help from Peter Ent, I created a USCAStates control. Here is how it could be used:

<!-- Display US States ComboBox -->
<ns1:USCAStates country="United States" />

<!-- Display CA Provinces ComboBox -->
<ns1:USCAStates country="Canada" />

And that country property can be change on the fly, forcing the list to refresh on demand based on whatever country name is passed. In my application I have another ComboBox containing countries. If United States is selected then the next ComboBox displays U.S. States, if Canada is selected then the next ComboBox displays Canadian Provinces, and if any other country is selected then the next ComboBox is disabled using enabled=false.

Here is the code for USCAStates:

<?xml version="1.0" encoding="utf-8"?>
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml"
            dataProvider="{internalDP}"
            labelField="@label">


    <mx:Script>
        <![CDATA[
        
            public static const USA:String = "United States";
            public static const CANADA:String = "Canada";
            
            // have the ComboBox look at this internal provider and switch it

            // depending on the value of country

            [Bindable]
            private var internalDP:XMLList;
            
            private var _country:String;
            
            public function set country(c:String):void
            {
                _country = c;
                
                if (_country==USA)
                {
                    internalDP = usstates.state;
                }
                else if (country==CANADA)
                {
                    internalDP = castates.state;
                }                
            }
            public function get country() : String
            {
                return _country; // just in case anyone wants to know

            }
        ]]>

    </mx:Script>

    <mx:XML id="usstates" xmlns="">
        <root>
            <state label="Alabama" data="AL"/>
            <state label="Alaska" data="AK"/>
            <state label="Arizona" data="AZ"/>
            <state label="Arkansas" data="AR"/>
            <state label="California" data="CA"/>
            <state label="Colorado" data="CO"/>
            <state label="Connecticut" data="CT"/>
            <state label="District of Columbia" data="DC"/>
            <state label="Delaware" data="DE"/>
            <state label="Florida" data="FL"/>
            <state label="Georgia" data="GA"/>
            <state label="Hawaii" data="HI"/>
            <state label="Idaho" data="ID"/>
            <state label="Illinois" data="IL"/>
            <state label="Indiana" data="IN"/>
            <state label="Iowa" data="IA"/>
            <state label="Kansas" data="KS"/>
            <state label="Kentucky" data="KY"/>
            <state label="Louisiana" data="LA"/>
            <state label="Maine" data="ME"/>
            <state label="Maryland" data="MD"/>
            <state label="Massachusetts" data="MA"/>
            <state label="Michigan" data="MI"/>
            <state label="Minnesota" data="MN"/>
            <state label="Mississippi" data="MS"/>
            <state label="Missouri" data="MO"/>
            <state label="Montana" data="MT"/>
            <state label="Nebraska" data="NE"/>
            <state label="Nevada" data="NV"/>
            <state label="New Hampshire" data="NH"/>
            <state label="New Jersey" data="NJ"/>
            <state label="New Mexico" data="NM"/>
            <state label="New York" data="NY"/>
            <state label="North Carolina" data="NC"/>
            <state label="North Dakota" data="ND"/>
            <state label="Ohio" data="OH"/>
            <state label="Oklahoma" data="OK"/>
            <state label="Oregon" data="OR"/>
            <state label="Pennsylvania" data="PA"/>
            <state label="Rhode Island" data="RI"/>
            <state label="South Carolina" data="SC"/>
            <state label="South Dakota" data="SD"/>
            <state label="Tennessee" data="TN"/>
            <state label="Texas" data="TX"/>
            <state label="Utah" data="UT"/>
            <state label="Vermont" data="VT"/>
            <state label="Virginia" data="VA"/>
            <state label="Washington" data="WA"/>
            <state label="West Virginia" data="WV"/>
            <state label="Wisconsin" data="WI"/>
            <state label="Wyoming" data="WY"/>
        </root>
    </mx:XML>

    <mx:XML id="castates" xmlns="">
        <root>
            <state label="Alberta" data="AB"/>
            <state label="British Columbia" data="BC"/>
            <state label="Manitoba" data="MB"/>
            <state label="New Brunswick" data="NB"/>
            <state label="Newfoundland" data="NL"/>
            <state label="Northwest Territories" data="NT"/>
            <state label="Nova Scotia" data="NS"/>
            <state label="Nunavut" data="NU"/>
            <state label="Ontario" data="ON"/>
            <state label="Prince Edward Island" data="PE"/>
            <state label="Quebec" data="QC"/>
            <state label="Saskatchewan" data="SK"/>
            <state label="Yukon" data="YT"/>
        </root>
    </mx:XML>

</mx:ComboBox>

[Update] Here is a simple usage example, sent to me by Dan Zeitman:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:ns1="*">

<mx:RadioButtonGroup id="Country" change="{cbStates.country = Country.selectedValue.toString()}"/>
<mx:RadioButton label="United States" groupName="Country" selected="true"/>
<mx:RadioButton label="Canada" groupName="Country"/>
<ns1:USCAStates country="United States" id="cbStates" />
</mx:Application>

Related Blog Entries

TrackBacks
There are no trackbacks for this entry.

No trackback URL. Trackbacks are only allowed via interactive form.

Comments
more extensible approach - let the state component filter based on the country id passed.

<?xml version="1.0" encoding="utf-8"?>
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml";>
<mx:Script>
   <![CDATA[
      import mx.collections.ArrayCollection;
      
       private var _countryID:uint;
      
       [Bindable]
       public function set countryID(id:uint):void {
          
             this._countryID = id;
             (this.dataProvider as ArrayCollection).filterFunction = filterData;
             (this.dataProvider as ArrayCollection).refresh();
             this.selectedIndex = 0;
      
       }
      
       public function get countryID():uint {
          
             return this._countryID;
       }
      
       private function filterData(val:Object):Boolean {
                
              return (val.data == this._countryID);
       }
   ]]>
</mx:Script>
</mx:ComboBox>

That's how it's used in the main app:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
            xmlns:miningComponents="com.satpara.components.*"
            layout="absolute"
            xmlns:satpara="http://www.satpara.com"; xmlns:local="*">
   <mx:ArrayCollection id="country">
      <mx:Array>
         <mx:Object label="United States" data="1"></mx:Object>
         <mx:Object label="Canada"   data="2"></mx:Object>
      </mx:Array>
   </mx:ArrayCollection>
   
   <mx:ArrayCollection id="statesProvinces" >
      <mx:Array>
         <mx:Object label="Colorado" data="1"></mx:Object>
         <mx:Object label="Kansas" data="1"></mx:Object>
         <mx:Object label="Massachusetts" data="1"></mx:Object>
         <mx:Object label="Oregon" data="1"></mx:Object>
         <mx:Object label="Washington" data="1"></mx:Object>
         <mx:Object label="Texas" data="1"></mx:Object>
         <mx:Object label="Florida" data="1"></mx:Object>
         <mx:Object label="New Hampshire" data="1"></mx:Object>
         <mx:Object label="British Columbia" data="2"></mx:Object>
         <mx:Object label="Alberta" data="2"></mx:Object>
         <mx:Object label="Manitoba" data="2"></mx:Object>
         <mx:Object label="Sasketchewan" data="2"></mx:Object>
         <mx:Object label="Ontario" data="2"></mx:Object>
         <mx:Object label="Quebec" data="2"></mx:Object>
         <mx:Object label="North West Territories" data="2"></mx:Object>
         <mx:Object label="Nunavut" data="2"></mx:Object>
      </mx:Array>
   </mx:ArrayCollection>
   <mx:ComboBox id="countriesCB" dataProvider="{country}" x="168" y="60"></mx:ComboBox>
   <local:StatesComboBox dataProvider="{statesProvinces}" countryID="{countriesCB.selectedItem.data as uint}"></local:StatesComboBox>
   <mx:Script>
      <![CDATA[
          private function filterStates(val:Object):Boolean {
             
             return val.id = countriesCB.selectedItem.data
          }
         
      ]]>
   </mx:Script>
</mx:Application>
# Posted By Abdullah | 11/11/09 1:22 AM

  © Copyright 1997-2009 Ben Forta, All Rights Reserved