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,.