You could write the Item Renderer as an MXML component or an
ActionScript class.
I have created a sample of both types that do the same thing. Sometimes
it easier to do it in MXML; Sometimes AS is the best way. With this
trivial example, both are about the same. Pick whichever way you like
the best.
DateRendererAS.as
package ItemRenderers
{
import mx.controls.*;
import mx.controls.dataGridClasses.DataGridListData;
import mx.formatters.DateFormatter;
public class DateRendererAS extends Label
{
private var myDateFormatter:DateFormatter = new DateFormatter();
public function DateRendererAS()
{
super();
myDateFormatter.formatString = "DD/MMM/YYYY";
}
override public function set data(value:Object):void
{
if(value != null)
{
super.data = value;
text =
myDateFormatter.format(value[DataGridListData(listData).dataField]);
}
}
}
}
DateRendererMXML.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.dataGridClasses.DataGridListData;
override public function set data(value:Object):void
{
if(value != null)
{
super.data = value;
text =
myDateFormatter.format(value[DataGridListData(listData).dataField]);
}
}
]]>
</mx:Script>
<mx:DateFormatter id="myDateFormatter" formatString="DD/MMM/YYYY"/>
</mx:Label>
And here is an application to try them out with (Change the itemRenderer
to DateRendererAS to try the AS version)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical">
<mx:Script>
<![CDATA[
private var dp:Array = [
{symbol: "ADBE", name: "Adobe Systems Inc.",
dateIncorporated: "3/1/50"},
{symbol: "MACR", name: "Macromedia Inc.",
dateIncorporated: "1/1/84"},
{symbol: "MSFT", name: "Microsoft Corp.",
dateIncorporated: "11/24/71"},
{symbol: "IBM", name: "IBM Corp.", dateIncorporated:
"9/14/63"}
];
]]>
</mx:Script>
<mx:DataGrid id="dg" initialize="dg.dataProvider = dp"
verticalAlign="middle">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name"
width="140"/>
<mx:DataGridColumn headerText="Symbol" dataField="symbol"
width="60" />
<mx:DataGridColumn headerText="Date"
dataField="dateIncorporated" width="100"
itemRenderer="ItemRenderers.DateRendererMXML" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
--- In [email protected], Angelo Anolin <angelo_ano...@...>
wrote:
>
> I do know a bit of using itemrenderer.. Inline that is..
>
> if you could post some example, I'd greatly appreciate it.
>
> would the itemrenderer be an as file or would it be inline as well?
>
> Thanks.
>
> Angelo
>
>
>
> ________________________________
> From: valdhor valdhorli...@...
> To: [email protected]
> Sent: Wed, 17 March, 2010 14:41:19
> Subject: [flexcoders] Re: Reuse a LabelFunction
>
>
> Use an itemRenderer instead.
>
> If you need help creating one, let me know.
>
> --- In flexcod...@yahoogro ups.com, Angelo Anolin <angelo_anolin@ ...>
wrote:
> >
> > Hi FlexCoders,
> >
> > I have created a labelfunction which I use in my datagrid to format
the display value.
> >
> > <mx:DateFormatter id="df" formatString= "DD/MM/YYYY" />
> >
> > And this is the AS function
> > private function labelFunctionTest( itm:Object, col:DataGridColumn)
:String
> > {
> > return df.format(itm. DataFieldNamefor Col1);
> > }
> >
> > Is there a way so that I could reuse the same label function to
other datagrid columns? As you can see from the script above, I am only
using the function to Col1 for the datagrid. How would I be able to
specify the datafield if I am going to reuse the same with other
datagrid columns?
> >
> > Thanks.
> >
>