Flex中使用ContextMenu - 添加,动态改变, 响应.

Categories: Flex; Tagged with: ; @ March 26th, 2009 11:31

AIR Only: AIR: 通过监听ContextMenuEvent.MENU_SELECT事件 动态改变菜单内容

\ 这里是通用的:

		_menu = new ContextMenu();
		_menuAddTopOU = new ContextMenuItem(RM.getString(BUNDLE_OUSETUP, "ous.menu.addTopOU"));
		_menuAdd = new ContextMenuItem(null, true);
		_menuEdit = new ContextMenuItem(null);
		_menuRemove = new ContextMenuItem(null);
		
		_menu.customItems.push(_menuAddTopOU);
		_menu.customItems.push(_menuAdd);
		_menu.customItems.push(_menuEdit);
		_menu.customItems.push(_menuRemove);
		_menu.addEventListener(ContextMenuEvent.MENU_SELECT, onMenuSelected);
		_menu.addEventListener(Event.SELECT, onMenuItemSelected);
		
		_treeOUs.contextMenu = _menu;

	// Menu打开后动态改变菜单内容
	private function onMenuSelected(event:ContextMenuEvent):void {
		...
		_menuAddTopOU.enabled = !tempNew; // add Top ou
		_menuAdd.enabled = (_treeOUs.selectedItem != null) && (!_editorOU.editable);	// add ou
		if(_menuAdd.enabled) {
			_menuAdd.label = RM.getString(BUNDLE_OUSETUP, "ous.menu.add", [currentOU.nameFullLocalized]);
		}else {
			_menuAdd.label = RM.getString(BUNDLE_OUSETUP, "ous.menu.selectOneFirst");
		}
		
		..
	}

	// 选中Menu中某item后响应
	private function onMenuItemSelected(event:Event):void {
		if(event.target == _menuAddTopOU) { // 增加顶级部门
			onAction(_actionAddTopOU, null);
		}else ...
	}

AIR: 通过监听ContextMenuEvent.MENU_SELECT事件 动态改变菜单内容

Categories: Flex; Tagged with: ; @ March 26th, 2009 9:47

某Tree上的menu

		_menu = new NativeMenu();
		_menuAddTopOU = new NativeMenuItem("Add Top OU");
		_menuAdd = new NativeMenuItem("Selcet one OU ls");
		_menu.addItem(_menuAddTopOU);
		_menu.addItem(_menuAdd);
		_menu.addEventListener(ContextMenuEvent.MENU_SELECT, onMenuSelected);
		_menu.addEventListener(Event.SELECT, onMenuItemSelected);
		_treeOUs.contextMenu = _menu;

监听函数:

	// Menu打开后动态改变菜单内容
	private function onMenuSelected(event:ContextMenuEvent):void {
		var currentOU:OrgUnit = _treeOUs.selectedItem as OrgUnit;
		_menuAdd.enabled = _actionAdd.enabled;
		if(_menuAdd.enabled) {
			_menuAdd.label = "Add OU in " + currentOU.nameFullLocalized;
		}else {
			_menuAdd.label = "Select one UP first";
		}
	}
	
	// 选中Menu中某item后响应
	private function onMenuItemSelected(event:Event):void {
		if(event.target == _menuAddTopOU) {
			trace("右键点击增加顶级部门");
			onAction(_actionAddTopOU, null);
		}else if(event.target == _menuAdd) {
			trace("增加子部门");
			onAction(_actionAdd, null);
		}
	}

使用ActionScript建立DataGrid, 添加右键, 增加列, 并设定列的ItemRenderer

Categories: Flex; Tagged with: ; @ January 6th, 2009 0:59

很多时候为了获得对组件的灵活控制, 不得不放弃MXML, 直接使用ActionScript.

 
//新建一个DataGrid _dataGrid = new DataGrid(); 
//增加右键 
var menu:NativeMenu = new NativeMenu(); 
var menuItemDelete:NativeMenuItem = new NativeMenuItem("Delete"); 
//监听事件,在右键选定Delete时运行该函数
 menuItemDelete.addEventListener(Event.SELECT, onMenuDeleteClicked); 
//将该menuItem加入到Menu中 
menu.addItem(menuItemDelete);
 _dataGrid.contextMenu = menu; //将菜单加入到DataGrid中 

columnLocale = new DataGridColumn(); //新建一个列 
columnLocale.dataField ="locale";//设定DataGrid中locale列的EditItem;
comboBoxLocaleEditor = new ClassFactory(ComboBox); // D
comboBoxLocaleEditor.properties = {dataProvider : LocalizationItem.localeArray} //设定该EditorItem的属性 
columnLocale.itemEditor = comboBoxLocaleEditor; 

var columnGender:DataGridColumn = new DataGridColumn();
 columnGender.headerText = "Gender""; 
columnGender.dataField = "Gender_"; 
//使用ItemRender 
columnGender.itemRenderer = new ClassFactory(Gender_Label);
 _dataGrid.showHeaders = false; //隐藏DataGrid的Header 
_dataGrid.columns = [columnLocale, columnGender]; //这一个数组,存放DataGrid的Column 

_dataGrid.dataProvider = value; //设定dataProvider 

具体在实际中的使用, 无异于MXML.

AIR中通过右键直接选定基于LIST容器[DataGrid, List, Tree等]的数据 – Select List item with mouse right-click

Categories: 垃圾山; Tagged with: ; @ January 5th, 2009 23:18

问题: 在很多情况下, 我们在DataGrid, List, Tree等容器中使用右键, 进行如修改, 删除 某行的操作.  问题是如果该容器初始状态下直接进行右键点击时, 并不能选定任何数据.

解决: 通过监听右键菜单SELECT事件, 获取到当前右键所击位置的Index, 并将之赋值给DataGrid或其他容器的SelectIndex, 完成点击操作

详细实现:  以DataGrid为例.

第一步, 首先给DataGrid添加菜单

			var dgMenu:NativeMenu = new NativeMenu();
			var del:NativeMenuItem  = new NativeMenuItem("Test");
			del.addEventListener(Event.SELECT, onRightClickDel);
			dgMenu.addItem(del);
			dgMenu.addEventListener(ContextMenuEvent.MENU_SELECT, onRightClicked);
			_dataGridStudent.contextMenu = dgMenu;

第二步, 响应函数:

	/**响应右键点击 */
	private function onRightClicked(e:ContextMenuEvent):void {
		var rightClickItemRender:IListItemRenderer;

		if(e.mouseTarget is IListItemRenderer) {
			rightClickItemRender = IListItemRenderer(e.mouseTarget);
		}else if(e.mouseTarget.parent is IListItemRenderer) {
			rightClickItemRender = IListItemRenderer(e.mouseTarget.parent);
		}

		if(rightClickItemRender != null) {
			var rightClickIndex:int = _dataGridStudent.itemRendererToIndex(rightClickItemRender);
			if(_dataGridStudent.selectedIndex != rightClickIndex) {
				_dataGridStudent.selectedIndex = rightClickIndex;
				onSlectedChange();
			}
		}
		trace("通过右键单击获得选定的行: " + _dataGridStudent.selectedIndex);
	}

Delete响应函数:

	/** 当右键菜单的Delete点击后响应*/
	private function onRightClickDel(e:Event):void {
		//在这里放逻辑....
	}

效果:

image

控制台Trace信息:

通过右键单击获得选定的行: 1

 

参考资料: Adobe CookBook  Select List item with mouse right-click



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.