May 14th, 2007 by Kyle
Tags: baselistdata, datagrid, datagridlistdata, Flex, idropinlistitemrenderer, item-renderer, itemrenderer, listdata, mxml, radio-button, radiobutton
Posted in: Flex
I recently had a request from a customer to demonstrate how a RadioButton in a DataGrid ItemRenderer could select and entire row and indicate that a row is selected. I also had a notion to put code on my blog that demonstrated a simple ItemRenderer that implemented mx.controls.listClasses.IDropInListItemRenderer since that allows you to access data and datagrid properties easily. I seem to often be looking around for a simple example to use as a starting point for many ItemRenderer examples I produce, but can never find a “template” starting point. This entry should take care of both these things.
Here is what I came up with:
Here is the code for a simple app that demos a datagrid (this is straight from the docs, but I added a bit more data to the grid):
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:script>
<!–[CDATA[
import mx.collections.ArrayCollection;
[Bindable]–> public var employees:ArrayCollection = new ArrayCollection([
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’},
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’},
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’},
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’},
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’},
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’},
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’},
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’},
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’},
{name: ‘Bob Jones’, phone: ‘413-555-1212′,
email: ‘bjones@acme.com’},
{name: ‘Sally Smith’, phone: ‘617-555-5833′,
email: ’ssmith@acme.com’}
]);
]]>
</mx:script>
<mx:panel title="DataGrid Control Example" height="100%" width="100%">
paddingTop="10" paddingLeft="10" paddingRight="10">
<mx:datagrid id="dg" width="100%" height="100%">
rowCount="5" dataProvider="{employees}" change="dg.invalidateList();">
<mx:columns>
<mx:datagridcolumn datafield="name" headertext="Name">
<mx:datagridcolumn datafield="phone" headertext="Phone">
<mx:datagridcolumn datafield="email" headertext="Email">
<mx:datagridcolumn width="80" textalign="center" editable="false">
headerText="Select" itemRenderer="RBRenderer"/>
</mx:datagridcolumn>
</mx:datagridcolumn>
<mx:form width="100%" height="100%">
<mx:formitem label="Name">
<mx:label text="{dg.selectedItem.name}">
</mx:label>
<mx:formitem label="Email">
<mx:label text="{dg.selectedItem.email}">
</mx:label>
<mx:formitem label="Phone">
<mx:label text="{dg.selectedItem.phone}">
</mx:label>
</mx:formitem>
</mx:formitem>
</mx:formitem>
</mx:form></mx:datagridcolumn></mx:datagridcolumn></mx:columns></mx:datagrid></mx:panel></mx:application>
Here is my itemRenderer, RBRenderer.mxml:
<mx:hbox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalalign="center">
implements="mx.controls.listClasses.IDropInListItemRenderer">
<mx:script>
<!–[CDATA[
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.BaseListData;
private var _listData:BaseListData;
private var myDG:DataGrid;
public function get listData():BaseListData {
return _listData;
}
public function set listData( value:BaseListData ):void {
_listData = value;
myDG = _listData.owner as DataGrid;
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if(myDG){
if(myDG.selectedItem){
if(myDG.selectedItem.mx_internal_uid == _listData.uid){
rdo.selected=true;
}
else{
rdo.selected=false;
}
}
}
}
]]–>
</mx:script>
<mx:radiobutton id="rdo" selected="false">
</mx:radiobutton>
</mx:hbox>
Note how I access the listData property and use it to access properties of the dataGrid and its data, as well as the data of the current item that is being renderer (very useful!).
Here is a link to a Flex Builder 2.0.1 project (compiled with SDK hotfix1) containing a sample demonstrating the solution described above.











February 6th, 2008 at 9:30 pm
Nice site keep it up!
————————————–
http://www.dasofte.com
June 23rd, 2008 at 2:34 am
hi,
Even i need to display a Radio button for every row,i followed the same procedure as specified here but i’m getting an error
Please find below the error which was thrown
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at css::RBRenderer/set listData()
at mx.controls::DataGrid/http://www.adobe.com/2006/flex/mx/internal::setupRendererFromData()
at mx.controls::DataGrid/mx.controls:DataGrid::commitProperties()
at mx.core::UIComponent/validateProperties()
at mx.managers::LayoutManager/::validateProperties()
at mx.managers::LayoutManager/::doPhasedInstantiation()
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/::callLaterDispatcher2()
at mx.core::UIComponent/::callLaterDispatcher()
June 23rd, 2008 at 3:27 am
I think i have figured out the problem, as i use a remote object as the Dataprovider for the DataGrid, while loading the file it looks for the data in the remote object which is right now empty and only gets filled up after the remote object is set with values. If my conclusion is right then how do i prevent this error from appearing ?? Please do provide some solution
July 11th, 2008 at 12:39 am
Actually you could do it a bit simpler like this:
July 11th, 2008 at 12:48 am
OK, so my code didn’t show up.
Basically, you just need a “set data” method and get the DataGrid like this:
var parent:DataGrid = owner as DataGrid;
If the parent is not null, set the radiobutton selected property like this:
radioButton.selected = (parent.selectedItem == value);
(It’s also important to use the
change=”dg.invalidateList();”
on the datagrid as you show (thanks!))
July 23rd, 2008 at 12:03 pm
i got this error when i click on the radiobutton: Property mx_internal_uid not found
Can you help me?
Thanks
July 23rd, 2008 at 12:41 pm
I solved it with:
if(myDG.selectedIndex == _listData.rowIndex)…