Dynamic itemRenderer using a ComboBox

Someone on flexcoders asked how you would create an itemRenderer which sometimes displayed a ComboBox while not displaying it other times. I decided to try and create this itemRenderer. Since the visibility of this ComboBox would be determined by the data, I created a custom itemRenderer that changes the visibility of a ComboBox and a Label in the set data function.

Here is the Demo and Code -

Demo: dynamicItemRendererExample.swf

Sample Code: dynamicItemRendererExample.mxml , DynRenderer.mxml

I did not choose to use an editable DataGridColumn because in this example, changing one of the ComboBoxes in the “Available Colors” column doesn’t actually change the data for the ‘colors’ property of the dataProvider which is the dataField for this column. Therefore, the default editable DataGrid code doesn’t work since that code expects that if I edit a cell in the “Available Colors” column, I am wanting to change the ‘colors’ property in some way. Instead, I am handling all changes to data in my itemRenderer. I am updating my DataGrid’s dataProvider on the change event of the ComboBox.

Note, that this is the way that I solved this problem. This doesn’t necessarily reflect best practice or anything. I’m not 100% sure what best practice for this scenario is. I’m just providing one solution. Thanks!

~ by jlafferty on September 11, 2007.

17 Responses to “Dynamic itemRenderer using a ComboBox”

  1. Thanks for that, but I would have been understood it even easily if there were some comments in the DynRenderer.mxml. This example really solved my problem

  2. [...] HereĀ  is a nice tutorial from a good and new “tech” blog. This article is about itemRenderer in a Flex dataGrid. Source are available. [...]

  3. I had some complex itemRenderers and itemEditors …
    Modifying the underlying DataGrid.dataProvider (ArrayCollection) wasnt automatically invoking the setData …

    DataGrid.dataProvider.refresh() ..works okay for a while then DATAgrid goes into a vegetative state after a couple of clicks.

    Thanks for this posting.
    I think DataGrid.dataProvider.itemUpdated is the ONLY proper solution to update both the ItemRenderer sitting on top of datgrid and underlying DataGrid.datProvider. (unless u can manage to get a handle to the ItemRenderer/ItemEditor which is Not recommended)

  4. Thanks Joan. Great of you to share your knowledge. Exactly the solution I needed.

  5. Perfect, this is exactly what I needed, and I think your solution was elegant enough, if you don’t have the column it still adds the proper data to the dataprovider. :-)

  6. I think your host provider is down. Anytime I try to view the source the connection times out to /butterfliesandbugs.bravehost.com/dynamicItemRendererExample.mxml.

  7. It should be up now, please try again. Thanks.

  8. Thank you (jlafferty). This really helped me.

  9. Hi I want to know one thing that I don’t figure out. I added a button and in the click event write the tarce(dg.selectedItem.color);

    I get all color what ever in the combo box how to get only the selected item.

    please let me know.

  10. I have an custom itemrenderer which i also use as an editor. I use states to transition from label to the custom component i.e. a textfield and an image button which opens up a popup. It works fine but when I edit the cell data my dataprovider doesnt refresh the custom components data.

  11. “Thanks Joan. Great of you to share your knowledge. Exactly the solution I needed.” nothing more

  12. Yeah, ok so I’m WAY behind on this conversation, but it’s close to what I need…just not quite.

    I’m doing something similar, rendering a different item depending on the data; however, in my case, the ComboBox I’m rendering is a custom component that has some built in pre-population behavior and change event handling. I’ve got it rendering ok, but the trick is, I can’t seem to figure out how to listen for the change event on the custom component from inside the custom renderer. I.E. when the combobox (custom component) is rendered, I can’t see to detect any changes a user might make.

    Thoughts?

  13. Nevermind…I got it. Trick was adding custom event metadata to the component and then dispatching that event as part of the component’s own “change” event handler. Then my renderer just has to listen for that custom event.

  14. [...] Dynamic ItemRenderer Using Combobox [...]

  15. [...] Dynamic ItemRenderer Using Combobox [...]

  16. Its really nice exmaple. its very helpfull for me.
    Thanx.

  17. May I suggest enabling the srcview attribute in your application tag in order to allow the right click > view source. It would be a lot cleaner and easier then the source you display. Otherwise, good example.

    http://livedocs.adobe.com/flex/3/html/help.html?content=build_7.html

Leave a Reply