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.

5 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!

  4. Thanks a lot !

  5. Tank you!! that was awesome!! wonderful!!
    Just for portuguese ans spanish speekers find the article in the blog, the word that refers the article in thas languages are:
    ” Como setar preencher popular combobox em um datagrid dinamicamente
    diferentes dataprovider para cada combobox diferentes dados
    Como llenar combobox en un datagrid dinamicamente cada combo con diferentes dataprovider datos diferentes ”
    I spend some time looking for that even in inglish…
    Tanks again!

Leave a Reply