加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:31:14 所属栏目:百科 来源:网络整理
导读:需求:能根据记录的紧急程度显示不同的颜色,如 紧急,该行要用红色表示。 MyDataGrid组件代码: package common.flash.datagrid{ import flash.display.Sprite;import mx.collections.ArrayCollection;import mx.controls.DataGrid;public class MyDataGrid

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

 

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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读