How to set a ComboBox itemEditor dataProvider at runtime

On a forum (probably flexcoders), someone recently asked how to set the dataProvider for a ComboBox itemEditor at runtime. They wanted to set a different dataProvider for each of the ComboBoxes depending on their data. So, I decided to put this quick example together using a DataGrid with inline itemEditors that are ComboBoxes.

The DataGridColumn itemEditor that uses a ComboBox looks like this:

<mx:DataGridColumn dataField=”city” headerText=”Destination City” editorDataField=”
selectedItem”>
<mx:itemEditor>
<mx:Component>
<mx:ComboBox initialize=”outerDocument.addData(event)” />
</mx:Component>
</mx:itemEditor>
</mx:DataGridColumn>

In the ComboBox’s initialize handler, we are calling a function that will set its dataProvider. Her is what the function addData looks like:

public function addData(event:FlexEvent) : void
{
if(dg.dataProvider[dg.selectedIndex]['country'] == “Italy”)
ComboBox(event.target).dataProvider = ItalianCities;
else
ComboBox(event.target).dataProvider = UKCities;

}

The function checks what the selected item, then, they set a dataProvider for the editor accordingly.

You can run the sample here: ComboBoxItemRendererSample.swf

In the example, click any row in the right side column to bring up the itemEditor that is a ComboBox.

Code: ComboBoxItemRendererSample.mxml

~ by jlafferty on October 10, 2008.

3 Responses to “How to set a ComboBox itemEditor dataProvider at runtime”

  1. Thanks. This example helped me… I needed to know how to set the dataProvider for a ComboBox itemEditor at runtime.

  2. Thank you thank you thank you!! I’ve been searching all week for how to set an inline itemEditor’s dataProvider to something in the main document. Turns out it was as simple as the outerDocument prefix.

    Thanks again!

  3. Thank you! I find it strange having to leave addData(event:FlexEvent) public, but it works!

Leave a Reply