flex DataGrid根据数据动态显示行颜色(重写DataGrid组件)

根据数据紧急程度,使用Flex自定义DataGrid组件,通过重写代码实现不同紧急级别行显示不同颜色,例如紧急记录以红色高亮显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:能根据记录的紧急程度显示不同的颜色,如 紧急,该行要用红色表示。

MyDataGrid组件代码:

package common.flash.datagrid
{   
	import flash.display.Sprite;
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;
	
	public class MyDataGrid extends DataGrid
	{
		
		private var _rowColorFunction:Function;
		private var _customed:Boolean;
		private var _customerColor:uint=0;
		
		public function MyDataGrid()
		{
			super();
		}
		
		override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number, color:uint, dataIndex:int):void
		{
			 if (_customed==true)
		  	 {
		         if(this._rowColorFunction != null)
			     {
				     if (dataIndex < this.dataProvider.length)
				     {
					      var item:Object=this.dataProvider.getItemAt(dataIndex);//设定颜色
					      color=this._rowColorFunction.call(this, item, color);
				     }
			      }
				  else
				  {
					   if (this._customerColor!=0)
					   {
						    if (dataIndex < this.dataProvider.length)
						    {
						    	color=this._customerColor;
						    }
					   }
				  }
			}
			super.drawRowBackground(s, rowIndex, y, height, color, dataIndex);
		} 
		
		override public function set columns(value:Array):void
		{
			super.columns = value;
		}
		
		override public function set dataProvider(value:Object):void
	    {
	        super.dataProvider = value;
	    }
	    
		public function set rowColorFunction(rowColorFunction:Function):void
		{
		this._rowColorFunction=rowColorFunction;
		}
		
		public function get rowColorFunction():Function
		{
		return this._rowColorFunction;
		}
		  
		public function set customed(customed:Boolean):void
		{
		 _customed=customed;
		}
		  
		public function get customed():Boolean
		{
		return _customed;
		}
		  
		public function set customerColor(customerColor:uint):void
		{
		 _customerColor=customerColor;
		}
		  
		public function get customerColor():uint
		{
		return _customerColor;
		}
	}
}

在mxml中调用如代码:

<custom:MyDataGrid  id="myDataGrid" customed="true"   rowColorFunction="showStatus" >
showStatus函数如下:

private function showStatus(item:Object, color:uint):uint
{
	if (item.status=="1") 
	{
		return 0x32f817;
	}
	else if (item.status=="2")
	{
		return 0x8ad5fc;
	}
	else if(item.status=="6") 
	{
		return 0xe7e3e3;
	}
	return color;
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值