views:

11739

answers:

4

I have a Flex ComboBox that gets populated by a dataprovider all is well...

I would now like to add a default " -- select a item --" option at the 0 index, how can I do this and still use a dataprovider? I have not seen any examples of such, but I can't imagine this being hard...

A: 

The way I've dealt with this in the past is to create a new collection to serve as the data provider for the combobox, and then I listen for changes to the original source (using an mx.BindingUtils.ChangeWatcher). When I get such a notification, I recreate my custom data provider.

I wish I knew a better way to approach this; I'll monitor this question just in case.

Matt Dillard
+19  A: 

If you don't need the default item to be selectable you can use the prompt property of ComboBox and set the selectedIndex to -1. That will show the string you set propmt to as the selected value until the user chooses another. It will not appear in the list of options, however.

Theo
A: 

I need the default item to be selectable. How can I do this?

You would set the selected item index to whatever index of the default item
Joel
A: 

I came across this problem today and wanted to share my solution.

I have a ComboBox that has an ArrayCollection containing Objects as it's dataprovider. When the application runs, it uses a RemoteObject to go out and get the ArrayCollection/Objects. In my event handler for that call I just have it append another object to the beginning of the ArrayCollection and select it:

var defaultOption:Object = {MyLabelField: "Select One"};
myDataProvider.addItemAt(defaultOption, 0);
myComboBox.selectedIndex = 0;

This is what my ComboBox looks like for reference:

<mx:ComboBox id="myComboBox" dataProvider="{myDataProvider}" labelField="MyLabelField" />