Adobe Flex: Set Selected Item In ComboBox

Oddly, Flex does not let you set an item to be selected in its ComboBox component. While the selectedIndex property can be used, you would have to make your own method to loop through the ComboBox’s dataProvider contents to get the index. Luckily, someone saved us the trouble of creating our own component extending ComboBox. Use the code below to be used as a substitute component for mx:ComboBox.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="utf-8"?>
<mx:ComboBox>
   <mx:Script>
      <![CDATA[
         private var _selectedValue:String;
         private var bSelectedValueSet:Boolean = false;
         private var bDataProviderSet:Boolean = false;
 
         // Override committ, this may be called repeatedly 
         override protected function commitProperties():void {
            // invoke ComboBox version 
            super.commitProperties();
 
            // If value set and have dataProvider 
            if (bSelectedValueSet && bDataProviderSet) {
               // Set flag to false so code won't be called until selectedValue is set again 
               bSelectedValueSet=false;
               // Loop through dataProvider 
               for (var i:int=0; i<this.dataProvider.length; i++) {
                  // Get this item's data 
                  var item:String = this.dataProvider[i].data;
 
                  // Check if is selectedValue 
                  if(item == _selectedValue) {
                     // Yes, set selectedIndex 
                     this.selectedIndex = i;
                     break;
                  }
               }
            }
         }
 
         // Trap dataProvider being set 
         override public function set dataProvider(o:Object):void {
            // invoke ComboBox version 
            super.dataProvider = o;
 
            // This may get called before dataProvider is set, so make sure not null and has entries 
            if (o!=null && o.length) {
               // Got it, set flag 
               bDataProviderSet = true;
            }
         }
 
         // set for selectedValue 
         public function set selectedValue(s:String):void {
            // Set flag 
            bSelectedValueSet = true;
            // Save value 
            _selectedValue = s;
            // Invalidate to force commit 
            invalidateProperties();
         }
      ]]>
   </mx:Script>
 
</mx:ComboBox>

To use this custom made ComboBox, if your component file is under the folder com/ui/ then you have to specify it in the xml namespace. Setting the item in the ComboBox to be selected can be done by calling the selectedValue property (see code below).

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:fx="com.ui.*">
   <fx:ComboBox id="cbx_1″ selectedValue="STRING_VALUE" width="150″ dataProvider="{DATA_PROVIDER_ID_HERE}"/>
<mx:Application>

Donations appreciated. Every little $ helps. Or click Google +1

Related Posts with Thumbnails

tags: ,

16 Responses to “Adobe Flex: Set Selected Item In ComboBox”

  1. 1
    andrew Says:

    …doesn’t work for me. Error when compiling.

  2. 2
    tech Says:

    er, what does the error say? the code above does not compile correctly. you need to supply the correct data provider info (that will be used to retrieve data from the database) and the string_value should be replaced as well.

  3. 3
    Websites tagged "boolean" on Postsaver Says:

    [...] - Biblioteken på samma väg som cd-skivor och faxar… saved by Rrezz2009-03-12 - Adobe Flex: Set Selected Item In ComboBox saved by vault0seven2009-03-09 - Boolean Data saved by guilhermend2009-03-08 - Be a Spotlight Ninja [...]

  4. 4
    Abs Says:

    doesn’t work for me either.

    i’m confused with the “STRING_VALUE”

    if fill the dataProvider is card.
    with the definition:

    public var cards:ArrayCollection = new ArrayCollection(
    [ {label:"Visa", data:1},
    {label:"MasterCard", data:2},
    {label:"American Express", data:3} ]);

    how can i choose “MasterCard”?
    i fill selectedValue=”MasterCard”, but nothing happen

  5. 5
    tech Says:

    hi, sorry i have not tried using ArrayCollection

  6. 6
    Willinos Says:

    Hi, this code work? Can you send a example to my mail?
    Thanks a lot !!

  7. 7
    tech Says:

    yes, this code works :)

  8. 8
    Kyle Ward Says:

    Hey! ……… i’m getting a
    ‘Error: Classes must not be nested.’ when trying to implement this……any ideas?

  9. 9
    tech Says:

    hey, sorry man. i have not touched flex ever since i posted the code. can’t help you. maybe someone else can give you a hint

  10. 10
    seema Says:

    Hope you are doing great… I have project in (web development) Flex ColdFusion and SQL. I need someone to work on that project from beginning to end. I want a person who I can email if there is a problem and the problem is fixed in 24 hour period. The project hasn’t started yet.
    But if you are interested then please let me know your hourly rate. I like the idea of invoice and will pay based on the same…
    This is for the person who can give attention to my project if not daily at least every other day…. So please think and let me know if interested…

  11. 11
    tech Says:

    sorry. dont know coldfusion so i cant help

  12. 12
    TA Says:

    Hi,

    I did it this way :

    combobox.prompt=”Value”;
    combobox.selectedIndex = -1;

  13. 13
    RavikumarGM Says:

    refer
    http://www.forta.com/blog/index.cfm/2006/11/22/Flex-ComboBox-With-selectedValue-Support

    for updated combo component..

  14. 14
    tech Says:

    hey thanks for this man

  15. 15
    sreeja Says:

    thank you so much.It helped me a lot.

  16. 16
    tech Says:

    you are welcome

Leave a Reply

Spam protection by WP Captcha-Free