Flex 自定义DataGrid实现根据条目某一属性值改变背景颜色
自定义拓展的DataGrid(as类)代码如下:
packageczgh.components
{
importflash.display.Sprite;
importmx.controls.DataGrid;
importmx.core.UIComponent;
publicclassOptionalDataGridextendsDataGrid
{
privatevar_rowColorFunction:Function;
privatevar_customed:Boolean;
privatevar_customerColor:uint=0;
publicfunctionOptionalDataGrid()
{
super();
}
overrideprotectedfunctiondrawRowBackground(s:Sprite,rowIndex:int,y:Number,height:Number,color:uint,dataIndex:int):void
{
color=0XFFFFFF;
if(this._rowColorFunction!=null)
{
if(dataIndex<this.dataProvider.length)
{
varitem:Object=this.dataProvider.getItemAt(dataIndex);//设定颜色
color=this._rowColorFunction.call(this,item,color);
}
}
super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);
}
overrideprotectedfunctiondrawHeaderBackground(headerBG:UIComponent):void
{
headerBG.setStyle("borderVisible","false");
}
publicfunctionsetrowColorFunction(rowColorFunction:Function):void
{
this._rowColorFunction=rowColorFunction;
}
publicfunctiongetrowColorFunction():Function
{
returnthis._rowColorFunction;
}
}
}
在mxml中实现自定义的datagrid并使用其rowColorFunction方法
//通过比较每条记录中dataField为act和stand的大小决定该条记录的背景颜色
privatefunctionsetCustomColor(item:Object,color:uint):uint
{
if(Number(item["act"])<Number(item["stand"]))
{
return0x7bbfea;
}
returncolor;
}