如何对flex改变DataGrid行背景色
1, 创建一个扩展自 mx.controls.DataGrid 的类。这个类可以是MXML文件或者ActionScript文件,你可以根据自己的习惯创建。
import mx.controls.*;
import flash.display.Sprite;
public class CustomRowColorDataGrid extends DataGrid
{
private var _rowColorFunction:Function;
public function CustomRowColorDataGrid()
{
super();
}
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
{
if(this.rowColorFunction != null)
{
if(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);
}
}
2, 在你的程序中用你新建的类替代 <mx:DataGrid>。
在 drawRowBackground 方法中你可以对数据做一些判断。
private function rowColorFunction(item:Object, color:uint):uint
{
if(AAA(item).type=='1'){
color = 0xffe281;
}
return color;
}
3,配置mxml中CustomRowColorDataGrid 的rowColorFunction=“{rowColorFunction}”。