Flex 自定义DataGrid实现根据条目某一属性值改变背景颜色
作者:WarmHope
时间:2026-07-22
浏览:1
通过重写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,非常实用。
作者最新文章
Excel转PDF一页只有几行?PDF页面缩放怎么调
2026-11-25 14:51
SHIZUKU安卓软件下载地址 SHIZUKU安卓版安装地址
2026-11-25 12:18
微信Windows版更新,可同步手机浏览及悬浮窗内容
2026-11-25 12:18
电脑微信存储满了怎么清理?5个方法快速释放空间!
2026-11-25 12:18
微信电脑版手机同步操作详解
2026-11-25 12:18
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多


































