Adobe Flex: Set Selected Item In ComboBox
Posted by tech on
October 22, 2008
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> |











December 24th, 2008 at 10:35 am
…doesn’t work for me. Error when compiling.
December 26th, 2008 at 4:38 pm
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.
March 19th, 2009 at 1:32 pm
[...] - 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 [...]
April 14th, 2009 at 12:50 am
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
April 14th, 2009 at 12:54 am
hi, sorry i have not tried using ArrayCollection
September 19th, 2009 at 5:10 pm
Hi, this code work? Can you send a example to my mail?
Thanks a lot !!
September 19th, 2009 at 5:19 pm
yes, this code works
October 14th, 2009 at 8:04 am
Hey! ……… i’m getting a
‘Error: Classes must not be nested.’ when trying to implement this……any ideas?
October 14th, 2009 at 8:13 am
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
May 4th, 2010 at 3:05 pm
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…
May 4th, 2010 at 6:30 pm
sorry. dont know coldfusion so i cant help
May 7th, 2010 at 2:27 pm
Hi,
I did it this way :
combobox.prompt=”Value”;
combobox.selectedIndex = -1;
June 4th, 2010 at 2:04 am
refer
http://www.forta.com/blog/index.cfm/2006/11/22/Flex-ComboBox-With-selectedValue-Support
for updated combo component..
June 4th, 2010 at 2:06 am
hey thanks for this man
December 5th, 2010 at 11:29 pm
thank you so much.It helped me a lot.
December 5th, 2010 at 11:35 pm
you are welcome