自定义拓展的DataGrid(as类)代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package czgh.components
{
import flash.display.Sprite;
 
import mx.controls.DataGrid;
import mx.core.UIComponent;
 
public class OptionalDataGrid extends DataGrid
{
private var _rowColorFunction:Function;
private var _customed:Boolean;
private var _customerColor:uint=0;
public function OptionalDataGrid()
{
super();
}
 
override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number, color:uint, dataIndex:int):void
{
color=0XFFFFFF;
 
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);
}
 
override protected function drawHeaderBackground(headerBG:UIComponent):void
{
headerBG.setStyle("borderVisible","false");
}
 
 
 
public function set rowColorFunction(rowColorFunction:Function):void
{
this._rowColorFunction=rowColorFunction;
}
 
public function get rowColorFunction():Function
{
return this._rowColorFunction;
}
 
 
}
}

在mxml中实现自定义的datagrid并使用 其rowColorFunction方法

?
1
2
3
4
5
6
7
8
9
10
//通过比较每条记录中dataField为act和stand的大小决定该条记录的背景颜色
private function setCustomColor(item:Object, color:uint):uint
{
if (Number(item["act"])<Number(item["stand"]))
{
return 0x7bbfea;
}
 
return color;
}