需要重写flex的DataGrid控件,下面直接贴代码。
重写DataGrid控件,AS文件
ColorDataGrid.as
{
import flash.display.Sprite;
import mx.controls.DataGrid;
public class ColorDataGrid extends DataGrid
{
public function ColorDataGrid()
{
super();
}
public var _rowColorFunction:Function;
public function set rowColorFunction(f:Function):void
{
this._rowColorFunction = f;
}
public function get rowColorFunction():Function
{
return this._rowColorFunction;
}
override protected function drawRowBackground(s:Sprite, rowIndex:int,
y:Number, height:Number, color:uint, dataIndex:int):void
{
var oldColor:uint = color;
if( this._rowColorFunction != null ){
if(dataProvider!=null && dataIndex < this.dataProvider.length ){
var item:Object = this.dataProvider.getItemAt(dataIndex);
color = this._rowColorFunction.call(this, item, color);
}
}
super.drawRowBackground(s, rowIndex, y, height, color, dataIndex);
}
}
}
下面是重写控件的使用
<JiHuaXiu:ColorDataGrid rowColorFunction="colorFunction">
<JiHuaXiu:columns>
</JiHuaXiu:columns>
</JiHuaXiu:ColorDataGrid>
需要在页面中给控件传递一个更改行背景的方法
public function colorFunction(item:Object,color:Number):Number
{
//这里是根据条件重新设置行背景颜色
if(item.TIANSHU<=10 && item.TIANSHU!="")
{
color=0xf4fd04;
}
return color;
}