Using a simple Item Renderer is very simple in flex. You may have seen the attribute a in a few different places called “itemRenderer”.
The itemRenderer is available to any ListBase Class… the ListBase class implements the IListItemRenderer Interface, any of the following by default implement the IListItemRenderer:
Button, ComboBox, Container, DataGridItemRenderer, DateField, Image, Label, ListBase, ListItemRenderer, MenuBarItem, MenuItemRenderer, NumericStepper, TextArea, TextInput, TileListItemRenderer, TreeItemRenderer.
To use an itemRenderer attribute in mxml you have to use Controls that are in one way or another related to the ListBase Class. A couple common examples would be the following controls:
<mx:DataGrid/>
<mx:List/>
<mx:Tree/>
etc…
By Default the itemRenderer is Null, but essentially it is used for displaying a little bit more about the data that is stored in a dataset.
For example, say you wanted to use a DataGrid that represents a shopping cart view, and want to have an image included in one of the columns to display the product, because we all know the saying “pictures are worth 1000 words” and it has proven itself true in many situations.
You would do this utilizing an ItemRenderer.
The example at the top is a rather simple one, to see what it does, simple open up the application, and click one of the buttons in the grid, you will get an Alert Box showing you the id that you clicked…
Although simple, a real world scenario could be a “Remove” Button in a shopping cart, that would remove the item from the grid.
<mx:DataGridColumn headerText=“”
dataField=“test_id” width=“80″>
<mx:itemRenderer>
<mx:Component>
<mx:HBox horizontalAlign=“center”>
<mx:Button label=“Details”
width=“75″
click=“outerDocument.getDetails(data.test_id)”/>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
When you use an ItemRenderer with a datagrid you automatically get an implicit public variable available to you called “data”.
You may also be wondering what in the world does outerDocument do? outerDocument is used to access variables that are not in the scope of the itemRenderer, there for, anything you try to access in the outerDocument must be declared as PUBLIC for it to be accessed. Please don’t think you are limited by the example in using buttons in a datagrid, this can be anything! Along with all of the components I listed about, you can create your own components as well, just make sure they implement the proper classes, I’ve done this but the custom components I made extended from something that already implemented the correct use of the IListItemRenderer Interface… (an example that might not work by copying and pasting, I’m just writing the code off the top of my head)
<mx:DataGridColumn headerText=“”
dataField=“test_id” width=“80″>
<mx:itemRenderer>
<mx:Component>
<mx:HBox horizontalAlign=“center”>
<mx:Image
source=“data.someSourceYouDefineInTheDataProver)”/>
<!–
your data.source would likely be an image source ie.
source:“assets/images/CedarPointeRules.jpg”
–>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
There are many examples of using the item renderer out there, and this is simply one more resource.
NOTE:(the following links are the same as the ones at the top, just here for easy access in case the page gets long)




15 users commented in " Using the ItemRenderer with a DataGrid in Flex "
Follow-up comment rss or Leave a TrackbackTeke care of the memory with itemRenderers, especially if you use Containers.
More info here :
http://blogs.adobe.com/aharui/2007/03/thinking_about_item_renderers_1.html
Thank you! No one had covered how to work buttons into datagrids, and they were giving me fits! Thanks for the simple and obvious solution.
Thanks buddy. The concept of outerDocument has helped me a lot. Had been breaking my head over for such a long time.
thanks for posting. helped me to.
You’re a lifesaver, man!
The one thing I don’t get is, Why is it so dang difficult to find straightforward answers to some of the most typical used scenarios?
Like some of the other commenters, I, too, struggled with this until I found your post. And now it seems so simple.
Kind of reminds me of playing golf. Every now and then you hit a great round, and then suddenly the whole game falls apart.
Thank you!
thanx man,
ur way to explain is very nice but it needed more explanation bcoz item rendrer is not easy to understand so plz give more descriptive example if u can i will be grateful to u……..
thanx man
can u give us an example of how to add a linkbutton and its label is changed in each row according to a dataField from my dataprovider?
really a great it help me out.Thanks
thank u very much for u r simple solution with outerDocument method which cured my head ache
You DO NOT want to use an HBox inside an itemrenderer because it will slow your grid down A LOT! You generally don’t want to use containers in itemrenderers if your displaying more than 4 or 5 rows at a time. It’s crazy that this is the case, just working with the grain.
Thank you so much for this! I couldn’t figure out for the life for me how to access click events with buttons placed in a dataGrid. Your example was simple, straight forward and well documented. Thanks again!
Hello, could you please help me to take a look at this problem?
here is the orignal code of mxml:
it shows the class cast error while I tried to use this application
How to use tab in itemrender of datagrid , I have more than 2 columns that have Item renderer test fields
[...] [1] Using the ItemRenderer with a DataGrid [...]
Thanks for your efforts, I tried this, however hitting issues, and tried in another way which is working for me, code posted at my blog
Leave A Reply