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

9 responses

  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!

  6. Hello!
    Beautifull the outerDocument!
    Thank’s!!

  7. Constantijn Visinescu | Reply

    @Martin Harrigan

    If you find it weird to make the addData function public you can also mark it internal 😉

  8. i created two combo box item editor one country and other one is state, when i select a country the respected state should be populated in state combo,

    please help me to solve this.

  9. nagammai ramasamy | Reply

    when i try to access mydg.selectedindex – i am getting only -1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: