My posts on the Flex 2 DataGrid renderers have received a fair amount of traffic and some questions on how to add and/or change the item renderers at runtime. I finally had a need to try this in a project so now I can share the ‘how’ with everyone else.
Right click to view the source.
- Step 1.
Create the basic item renderer. One of the things I needed to accomplish with my item renderer was the ability to add it to different columns (ie the dataField was not always the same). This meant I needed a way from within the renderer to determine what column it was bound to so I could get and display the correct data. To do this the renderer needs to implement the
IDropInListItemRenderer. This interface allows the renderer to have access to information about the list and column it is in via theBaseListDataandDataGridListDataclasses. TheDataGridListDatagives you everything you need to get the data required to make a flexible, reusable renderer.Here is my basic image item renderer. Nothing to exciting, but it is very reusable. I can drop this into any datagrid to render an image column and it will work.
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" implements="mx.controls.listClasses.IDropInListItemRenderer"> <mx:Script> <![CDATA[ import mx.controls.dataGridClasses.DataGridListData; import mx.controls.listClasses.BaseListData; // Make the listData property bindable. [Bindable("dataChange")] private var _listData : BaseListData; public function get listData() : BaseListData { return _listData; } public function set listData( value : BaseListData ) : void { _listData = value; } ]]> </mx:Script> <mx:Image source="{data[(DataGridListData(listData).dataField)]}"/> </mx:Canvas> - Step 2.
Modify the renderer so that it can be added at runtime. To do this the renderer needs to implement the
IFactoryinterface. This will allow the renderer to be added at any point during runtime. Without implementing this interface it won’t work, period. Below is the code to implement the interface.<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" implements="mx.controls.listClasses.IDropInListItemRenderer, mx.core.IFactory">
public function newInstance():* { return new ImageRenderer(); } - Step 3.
The code to change a column at runtime from a basic column to an item renderer column. Get the columns from the datagrid, modify the column you are interested in and then reassign the columns to the datagrid. Notice that when you set the column’s
itemRenderervalue you must create a new instance of the desired item renderer.private function switchStatusColumn(event:MouseEvent):void { var cols:Array = users_dg.columns; var col:DataGridColumn = cols[1] as DataGridColumn; col.itemRenderer = new CheckBoxRenderer(); users_dg.columns = cols; } - Step 4.
The code to add a new column with an item renderer at runtime. This is almost identical to modifying an existing column. The only difference is that we push the new column into the columns array.
private function addImageColumn(event:MouseEvent):void { var cols:Array = users_dg.columns; var col:DataGridColumn = new DataGridColumn(); col.itemRenderer = new ImageRenderer(); col.dataField = 'emoticon'; col.headerText = 'Image'; cols.push(col); users_dg.columns = cols; }
Hope this helps all those who were asking how to do this.
I would love to see this extended so that when you click on a line it toggles the checkbox on and off.
Please could you show us how
Many thanks
BOb
Same as Bob’s request, I was trying to use itemRenderer to dynamically change the display of selected items. But not succeed. I can changed the whole column but not the selected items. If you can show us how to make a checkbox on and off I think I can make it work on the itemRenderer.
Hi Derrik,
I was trying to use itemRenderer to dynamically change the display of selected items.Like i need to increase the font of whole row if a column is clicked.Canu give any suggestion how to go for it .
thanx and regards
Sharad Srivastava
The transition on that looks really smooth, way beyond me coding wise though
THANKS THANKS THANKS!!!
Is beautiful this code…
Using this code I get 2 warnings about data binding… One – not being able to detect assignments to dataField, and the other, not being able to detect changes when using square bracket operator. Any thoughts?
same as Mukul’s comment how do I over come
Thanks a lot, I appreciate it.
As posted earlier, I get following data binding errors:
Data binding will not be able to detect changes when using square bracket operator. For Array, please use ArrayCollection.getItemAt() instead.
Data binding will not be able to detect assignments to “listData”.
Data binding will not be able to detect assignments to “dataField”.
Any ideas to solve this?
THANKS THANKS THANKS!!!²
Nice thanks
Hi I have one problem now i have the one List which has the item renderer as the custom class which basically nothing but the checkbox now after saving all this information to the backend i am retrieving this information again and opening the same window but now i want to know how to select the check box with the reference of item renderer
@Aakash, if you have the data in an ArrayCollection and the value bound against the checkbox is bindable then simply changing the value in the data object with make the check box select/unselect if you setup the data binding correctly in the custom renderer.