flex学习笔记-DataGrid,ComboBox AppEvent EventBus

事件:父页面添加监听子页面触发这个时候需要设置监听的第三个参数为true

		<!--flex4画线-->
		<s:Spacer height="100%"/>
		<s:Line width="100%">
			<s:stroke>
				<s:SolidColorStroke color="0x000000" weight="1" caps="square"/>
			</s:stroke>
		</s:Line>
<mx:DataGrid width="100%" height="100%" dataProvider="{list}">
	<mx:columns>
		<mx:DataGridColumn dataField="VEHICLE_LOCATION" headerText="位置"/>
		<mx:DataGridColumn dataField="VEHICLE_STATUS" headerText="状态"/>
		<mx:DataGridColumn headerText="时间" labelFunction="myLabelFunctionDG"/>
		<mx:DataGridColumn dataField="GPS_SPEED" headerText="速度"/>
		<mx:DataGridColumn dataField="GPS_DIRECTION" headerText="方向"/>
	</mx:columns>
</mx:DataGrid>

			<s:ComboBox id="category" dataProvider="{list}" 
						width="200"
						labelFunction="myLabelFunction"
						keyUp="setNextFocus_keyUpHandler(event)" 
						change="category_changeHandler(event)" />

			protected function setNextFocus_keyUpHandler(event:KeyboardEvent):void
			{
				if (event.keyCode == Keyboard.ENTER)
				{
					focusManager.setFocus(focusManager.getNextFocusManagerComponent());
				}
			}
			protected function enter_keyUpHandler(event:KeyboardEvent):void
			{
				if (event.keyCode == Keyboard.ENTER)
				{
					event.target.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
				}
			}
			protected function category_changeHandler(event:IndexChangeEvent):void
			{
				if(-3 == event.newIndex)
				{
					category.selectedIndex = event.oldIndex;
				}
			}
			
			private function myLabelFunction(item:Object):String
			{
				if(null == item || instanceOf(String).matches(item))
				{
					item = category.dataProvider.getItemAt(category.selectedIndex);
				}
				return item.value+"_"+item.label;
			}

			private function myLabelFunctionDG(item:Object, column:DataGridColumn):String
			{
				var track:TrackModel = item as TrackModel;
				return Util.dateFormat(track.CREATE_TIME, Util.FORMAT_FULL);
			}

package
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/**事件管理类,负责总事件的分发*/
	public class EventBus extends EventDispatcher
	{
		public function EventBus()
		{
			if(manager) throw new Error("EventBus is must be singleton!");
		}
		
		private static var manager:EventBus;
		
		public static function getInstance():EventBus
		{
			if(!manager)manager = new EventBus();
			return manager;
		}
		public function dispatch(type:String):Boolean
		{
			return dispatchEvent(new Event(type));
		}
	}
}

appevent类,这个类中重要的是重写clone方法
package
{
	import flash.events.Event;
	
	/**自定义事件,可以在data属性传递一个object值*/
	public class AppEvent extends Event
	{
		/**登录成功事件*/
		public static const LOGIN_SUCCESS:String = "loginSuccess";
		/**登录失败事件*/
		public static const LOGIN_FAULT:String = "loginFault";
		/**注册成功事件*/
		public static const REGISTER_SUCCESS:String = "registerSuccess";
		/**注册失败事件*/
		public static const REGISTER_FAULT:String = "registerFault";
		/**关闭弹出窗口事件*/
		public static const TITLE_WINDOW_CLOSE:String = "menuTitleWindowClose";
		/**地图加载完毕事件*/
		public static const MAP_LOADED:String = "googleMapLoaded";
		
	    public function AppEvent(type:String, data:Object = null, callback:Function = null)
	    {
	        super(type, false, false);
	        _data = data;
	        _callback = callback;
	    }
		
	    private var _data:Object;
	
	    private var _callback:Function;
	
	    public function get data():Object
	    {
	        return _data;
	    }
		
	    public function set data(value:Object):void
	    {
	        _data = value;
	    }
		
	    public function get callback():Function
	    {
	        return _callback;
	    }
		
	    public function set callback(value:Function):void
	    {
	        _callback = value;
	    }
		
	    public override function clone():Event
	    {
	        return new AppEvent(this.type, this.data);
	
	    }
	}
}

你可能感兴趣的:(datagrid)