导读:ib
render的构建曾经深深缠绕过我,这里我介绍一篇关于这方面的文章,看看作者的态度。我个人觉得可以用MXML解决的代码不必要动用AS,当然我也非常同意作者的观点AS更加强大,更加体现程序员的价值
I was recently working with item renderers on a project and wanted to post an example of using an ActionScript (class) based item renderer and compare it with an MXML version.
It is nice to have the option to choose either, but I like the control an ActionScript class gives the coder and it tends to yield a more lightweight approach than MXML.
可以很方便的选择使用MXML或者AS,但是我更倾向于使用AS因为它提供了更加轻量级的解决方案
My first code block below is the AS class approach. The renderer simply applies a currency format to a field of numbers in a data grid. The second is utilizing the MXML approach. As you can see they yield the same results.
下面的第一个代码块是使用AS实现的,RENDER也很简单
Notice how both approaches override the data setter for the label class they extend. Also see how how listData is utilized to associate the drop-in item renderer with the data from the list control.
注意两个方法都覆盖了LABEL的DATA SETTER,同时要注意到LISTDATA被利用来和DROP IN ITEM RENDER 联系起来控制LIST 控件
With this rather basic example you can see how any control can be displayed via MXML or ActionScript and drawn out i.e. rendered in a list based class.
I have attached a zippedproject that displays 2 grids using both renderers. I hope this helps you better understand the power of item renders and what you can do with them.
可以到作者的网站去下载这两种方法的项目代码
CurrencyRendererAS Class
package renderers
{
import mx.controls.Label;
import mx.controls.dataGridClasses.DataGridListData;
import mx.formatters.CurrencyFormatter;
public class CurrencyRendererAS extends Label
{
private var cf : CurrencyFormatter = new CurrencyFormatter();
public function CurrencyRendererAS()
{
super();
// set precision for formattercf.precision = 2;
}
// Override the set method for the data property.override public function set data(value:Object):void {
super.data = value;
if (value != null)
{
this.text = cf.format(value[DataGridListData(this.listData).dataField]);
this.setStyle("textAlign","right");
}
super.invalidateDisplayList();
}
}
}
CurrencyRendererMXML MXML
width="100%"> width="100%">
import mx.controls.Label;
import mx.controls.dataGridClasses.DataGridListData;
import mx.formatters.CurrencyFormatter;
override public function set data(value:Object):void {
super.data = value;
if (value != null)
this.text = cf.format(value[DataGridListData(this.listData).dataField]);
this.setStyle("textAlign""right"
super.invalidateDisplayList(); import mx.controls.Label;
import mx.controls.dataGridClasses.DataGridListData;
import mx.formatters.CurrencyFormatter;
override public function set data(value:Object):void {
super.data = value;
if (value != null)
this.text = cf.format(value[DataGridListData(this.listData).dataField]);
this.setStyle("textAlign""right"
super.invalidateDisplayList();
本文转自
http://blog.strikefish.com/blog/index.cfm/2008/1/9/Render-Me-This