欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义DataGrid

程序员文章站 2022-07-13 17:14:21
...
package cw.controls {
	import fl.controls.DataGrid;

	import flash.events.MouseEvent;

	import fl.controls.dataGridClasses.HeaderRenderer;

	import flash.events.Event;

	import fl.controls.dataGridClasses.DataGridColumn;

	import flash.display.Sprite;

	import fl.controls.listClasses.CellRenderer;

	import flash.display.DisplayObject;

	/**
	 * @author 25swf
	 * @site www.25swf.com
	 */
	public class DataGrid25 extends DataGrid {
		private var current_moveing_header : HeaderRenderer;
		private var current_moveing_row : Sprite;
		private var drop_column_id : int = -1;
		private var current_highLine : Sprite;
		private var current_selected_row : int = -1;
		public var highLineColor : Number = 0xFF0000;
		public var highLineBold : Number = 2;

		private var m_moverow : Boolean = true;
		private var m_movecolumn : Boolean = true;

		/**
		 * 获取是否可以移动row
		 */
		public function get moverow() : Boolean {
			return m_moverow;
		}

		/**
		 * 设置是否可以移动row
		 */
		public function set moverow(_b : Boolean) : void {
			m_moverow = _b;
		}

		/**
		 * 获取是否可以移动列
		 */
		public function get movecolumn() : Boolean {
			return 	m_movecolumn;
		}

		/**
		 * 设置是否可以移动列
		 */
		public function set movecolumn(_b : Boolean) : void {
			m_movecolumn = _b;	
		}

		public function DataGrid25() {
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}

		private function onMouseDown(e : MouseEvent) : void {
			if(e.target is HeaderRenderer && m_movecolumn) {
				var tempHeaderRender : HeaderRenderer = HeaderRenderer(e.target);
				stage.addChild(tempHeaderRender);
				tempHeaderRender.scaleX = 0.5;
				tempHeaderRender.scaleY = 0.5;
				tempHeaderRender.x = stage.mouseX + 2;
				tempHeaderRender.y = stage.mouseY + 2;
				current_moveing_header = tempHeaderRender;
				tempHeaderRender.startDrag();
				this.invalidate();
				stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			}else if(e.target is CellRenderer && m_moverow) {
				var tempCellRenderer : CellRenderer = CellRenderer(e.target);
				current_selected_row = tempCellRenderer.listData.row;
				stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				var tempDataGrid : DataGrid = new DataGrid();
				tempDataGrid.headerHeight = 0;
				tempDataGrid.width = this.width;
				tempDataGrid.height = this.rowHeight;
				tempDataGrid.columns = this.columns;
				tempDataGrid.addItem(this.getItemAt(current_selected_row));
				var tempSprite : Sprite = new Sprite();
				tempSprite.addChild(tempDataGrid);
				tempSprite.scaleX = 0.5;
				tempSprite.scaleY = 0.5;
				tempSprite.x = stage.mouseX + 2;
				tempSprite.y = stage.mouseY + 2;
				stage.addChild(tempSprite);
				tempSprite.startDrag();
				current_moveing_row = tempSprite;
			}
		}

		private function onMouseUp(e : Event = null) : void {
			if(current_moveing_header != null) {
				if(drop_column_id != -1) {
					var tempColumn : DataGridColumn = this.getColumnAt(current_moveing_header.column);
					this.removeColumnAt(current_moveing_header.column);
					this.addColumnAt(tempColumn, drop_column_id);
				}
				current_moveing_header.stopDrag();
				stage.removeChild(current_moveing_header);
				current_moveing_header = null;
				this.invalidateList();
			}
			clearHighLine();
			if(current_selected_row != -1) {
				current_selected_row = -1;
				clearRowCopy();
			}
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		}

		private function onMouseOver(e : Event) : void {
			if(e.target is HeaderRenderer && current_moveing_header != null) {
				var tempHeaderRender : HeaderRenderer = HeaderRenderer(e.target);
				if(drop_column_id == tempHeaderRender.column) {
					//如果是同一个,则可以直接终止往下执行了
					return;
				}
				drop_column_id = tempHeaderRender.column;
				//清除高亮
				clearHighLine();
				//高亮列
				var mySprit : Sprite = new Sprite();
				mySprit.graphics.lineStyle(highLineBold, highLineColor);
				mySprit.graphics.drawRect(0, 0, tempHeaderRender.width, this.height);
				mySprit.x = tempHeaderRender.x;
				current_highLine = mySprit;
				this.addChild(mySprit);
			}else {
				clearHighLine();
				drop_column_id = -1;	
			}
			if(e.target is CellRenderer && current_selected_row != -1) {
				var tempCellRenderer : CellRenderer = CellRenderer(e.target);
				if(current_selected_row == tempCellRenderer.listData.row) {
					//如果是同一个,则可以直接终止往下执行了
					return;
				}
				if(current_selected_row > tempCellRenderer.listData.row) {
					this.addItemAt(this.getItemAt(current_selected_row), tempCellRenderer.listData.row);
					this.removeItemAt(current_selected_row + 1);
				}
				else {
					this.addItemAt(this.getItemAt(current_selected_row), tempCellRenderer.listData.row + 1);
					this.removeItemAt(current_selected_row);
				}
				current_selected_row = tempCellRenderer.listData.row;
			}
		}

		/**
		 * 清除高亮
		 */
		private function clearHighLine() : void {
			if(current_highLine != null && current_highLine.parent != null) {
				current_highLine.parent.removeChild(current_highLine);		
				current_highLine = null;
			}	
		}

		/**
		 * 清除ROW的复制品
		 */
		private function clearRowCopy() : void {
			if(current_moveing_row != null && current_moveing_row.parent != null) {
				current_moveing_row.parent.removeChild(current_moveing_row);		
				current_moveing_row = null;
			}	
		}
	}
}

 

相关标签: Flash UP