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!


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
[...] HereĀ is a nice tutorial from a good and new “tech” blog. This article is about itemRenderer in a Flex dataGrid. Source are available. [...]
Hleinar ehf. » Blog Archive » Dynamic itemRenderer using a ComboBox said this on October 17, 2007 at 1:19 pm |
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)
Thanks Joan. Great of you to share your knowledge. Exactly the solution I needed.
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.
I think your host provider is down. Anytime I try to view the source the connection times out to /butterfliesandbugs.bravehost.com/dynamicItemRendererExample.mxml.
It should be up now, please try again. Thanks.
Thank you (jlafferty). This really helped me.
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.
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.
“Thanks Joan. Great of you to share your knowledge. Exactly the solution I needed.” nothing more
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?
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.
[...] Dynamic ItemRenderer Using Combobox [...]
ViewFactory as a Solution for Dynamic ItemRenderers | diamondTearz said this on May 5, 2009 at 5:19 pm |
[...] Dynamic ItemRenderer Using Combobox [...]
diamondTearz » ViewFactory as a Solution for Dynamic ItemRenderers said this on June 10, 2009 at 2:05 am |
Its really nice exmaple. its very helpfull for me.
Thanx.
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