Flex 自定义DataGrid实现根据条目某一属性值改变背景颜色
作者:WarmHope
时间:2026-07-22
浏览:0
通过重写DataGrid的drawRowBackground方法,自定义组件可根据回调函数返回的颜色动态改变行背景色。示例中,回调函数比较每条记录的act与stand字段,若act小于stand则将行底色设为浅蓝色,否则保留默认白色。
本文目录
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里用起来也很直观。这里用了一个简单的回调函数,通过比较每条记录里`act`和`stand`字段的值,来决定这一行的背景色。
// 如果act的值小于stand,就把这一行的底色设成浅蓝色,否则就保留默认颜色
private function setCustomColor(item:Object, color:uint):uint {
if (Number(item["act"])
这样一来,你就有了一个可以根据数据自动变色的DataGrid,非常实用。
作者最新文章
Word2016怎么移除文档空白页
2026-10-16 22:30
endnote怎么引用中文参考文献格式设置教程
2026-10-14 17:25
sql条件判断字符串不包含某个字符串的方法详解
2026-10-13 17:39
漫蛙漫画官网
2026-10-13 13:19
漫蛙官网入口免费下载安装最新版
2026-10-13 13:18
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多










































