Question

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...

Was it helpful?

Solution

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.

OTHER TIPS

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" />

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.

This can be used following code for selected default value of combobox

var index:String = "foo";
for(var objIndex:int = 0; objIndex < comboBox.dataProvider.length; objIndex++) {
  if(comboBox.dataProvider[objIndex].label == index)
  {
     comboBox.selectedIndex = objIndex;
     break;
  }
}
<mx:ComboBox id="comboBox" dataProvider="{_pageIndexArray}" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top