RIA Developer, Flex / Flash, Widgets
Tip of the Day – How to create a prompt field on your mx:ComboBox
For a long time I’ve been doing things like this within Flex.
[Bindable] private var _skills:Array = [ {label: "---", id: -1}, {label: "1 - Newbie", id: 1}, {label: "2 - Some Experience", id: 2}, {label: "3 - Expert", id: 3}, {label: "4 - I'm a baller", id: 4} ];
And then binding this data provider to a ComboBox…
<mx:ComboBox id="skillsCB" dataProvider="{_skills}" />Well it turns out there is a much easier way of doing this that I never knew of until today. ComboBox has a “prompt” attribute that you can set. It’s so easy…
<mx:ComboBox id="skillsCB" dataProvider="{_skills}" prompt="---" />I told you it was easy. Can’t believe I never noticed it before.
| Print article | This entry was posted by Nate Beck on April 18, 2009 at 12:55 am, and is filed under Actionscript, Flex, Tip of the day. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 1 year ago
Don’t forget to add the “selectedIndex=-1″ setter if you use the MXML method to ensure that the prompt is displayed if your dataProvider is populated through a service result handler function. And also that the prompt value can’t be selected again after you select another value unless your user knows how to deselect (control-click) — not necessarily intuitive to all — or they have another means of resetting the selected value to -1 via an actionscript function.
about 1 year ago
Hmmm…I never knew that you could ctrl-click on the combobox to reset that value to -1. I knew about the prompt, but wow…learn something new every day
Sounds like that might be a nice custom control though. DeselectableComboBox – onclick – if ( selectedItem == currentTarget ), either throw a ctrl-click or just set selectedIndex = -1.