在Flex中给datagrid添加右键菜单项的具体实现
程序员文章站
2022-06-19 13:14:39
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<s:application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationcomplete="initapp()">
<s:layout>
<s:horizontallayout horizontalalign="center" verticalalign="middle"/>
</s:layout>
<fx:script>
<![cdata[
import mx.controls.alert;
import mx.events.datagridevent;
import mx.events.flexevent;
import spark.events.gridevent;
private var mycontextmenu:contextmenu;
private function initapp():void
{
popupmenu();
}
private function popupmenu():void
{
mycontextmenu = new contextmenu();
removedefaultitems();
addcustommenuitems();
//监听右键菜单弹出后的事件
mycontextmenu.addeventlistener(contextmenuevent.menu_select, menuselectedhandler);
mydatagrid.contextmenu = mycontextmenu;
}
//删除原有菜单项
private function removedefaultitems():void
{
mycontextmenu.hidebuiltinitems();
var defaultitems:contextmenubuiltinitems = mycontextmenu.builtinitems;
defaultitems.print = false;
}
//添加用户自定义菜单项
private function addcustommenuitems():void
{
var item:contextmenuitem = new contextmenuitem("删除行");
//默认的菜单项
item.visible = false;
mycontextmenu.customitems.push(item);
//监听选择菜单项后的事件
item.addeventlistener(contextmenuevent.menu_item_select, itemselectedhandler);
}
//弹出菜单后的事件处理函数
private function menuselectedhandler(event:contextmenuevent):void
{
var item:contextmenuitem = mycontextmenu.customitems[0] as contextmenuitem;
if (mydatagrid.selectedindex >= 0)
{
item.visible = true;
}
else
{
item.visible = false;
}
}
//选择菜单项后的事件处理函数
private function itemselectedhandler(event:contextmenuevent):void
{
if (mydatagrid.selectedindex >= 0)
{
mydatagrid.dataprovider.removeitemat(mydatagrid.selectedindex);
}
}
]]>
</fx:script>
<s:datagrid requestedrowcount="4" id="mydatagrid"
width="500">
<s:columns>
<s:arraylist>
<s:gridcolumn datafield="datafield1" headertext="列 1" id="col1"></s:gridcolumn>
<s:gridcolumn datafield="datafield2" headertext="列 2"></s:gridcolumn>
<s:gridcolumn datafield="datafield3" headertext="列 3"></s:gridcolumn>
</s:arraylist>
</s:columns>
<s:typicalitem>
<fx:object datafield1="示例数据" datafield2="示例数据" datafield3="示例数据"></fx:object>
</s:typicalitem>
<s:arraylist>
<fx:object datafield1="数据1" datafield2="数据1" datafield3="数据1"></fx:object>
<fx:object datafield1="数据2" datafield2="数据2" datafield3="数据2"></fx:object>
<fx:object datafield1="数据3" datafield2="数据3" datafield3="数据3"></fx:object>
<fx:object datafield1="数据4" datafield2="数据4" datafield3="数据4"></fx:object>
</s:arraylist>
</s:datagrid>
</s:application>
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<s:application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationcomplete="initapp()">
<s:layout>
<s:horizontallayout horizontalalign="center" verticalalign="middle"/>
</s:layout>
<fx:script>
<![cdata[
import mx.controls.alert;
import mx.events.datagridevent;
import mx.events.flexevent;
import spark.events.gridevent;
private var mycontextmenu:contextmenu;
private function initapp():void
{
popupmenu();
}
private function popupmenu():void
{
mycontextmenu = new contextmenu();
removedefaultitems();
addcustommenuitems();
//监听右键菜单弹出后的事件
mycontextmenu.addeventlistener(contextmenuevent.menu_select, menuselectedhandler);
mydatagrid.contextmenu = mycontextmenu;
}
//删除原有菜单项
private function removedefaultitems():void
{
mycontextmenu.hidebuiltinitems();
var defaultitems:contextmenubuiltinitems = mycontextmenu.builtinitems;
defaultitems.print = false;
}
//添加用户自定义菜单项
private function addcustommenuitems():void
{
var item:contextmenuitem = new contextmenuitem("删除行");
//默认的菜单项
item.visible = false;
mycontextmenu.customitems.push(item);
//监听选择菜单项后的事件
item.addeventlistener(contextmenuevent.menu_item_select, itemselectedhandler);
}
//弹出菜单后的事件处理函数
private function menuselectedhandler(event:contextmenuevent):void
{
var item:contextmenuitem = mycontextmenu.customitems[0] as contextmenuitem;
if (mydatagrid.selectedindex >= 0)
{
item.visible = true;
}
else
{
item.visible = false;
}
}
//选择菜单项后的事件处理函数
private function itemselectedhandler(event:contextmenuevent):void
{
if (mydatagrid.selectedindex >= 0)
{
mydatagrid.dataprovider.removeitemat(mydatagrid.selectedindex);
}
}
]]>
</fx:script>
<s:datagrid requestedrowcount="4" id="mydatagrid"
width="500">
<s:columns>
<s:arraylist>
<s:gridcolumn datafield="datafield1" headertext="列 1" id="col1"></s:gridcolumn>
<s:gridcolumn datafield="datafield2" headertext="列 2"></s:gridcolumn>
<s:gridcolumn datafield="datafield3" headertext="列 3"></s:gridcolumn>
</s:arraylist>
</s:columns>
<s:typicalitem>
<fx:object datafield1="示例数据" datafield2="示例数据" datafield3="示例数据"></fx:object>
</s:typicalitem>
<s:arraylist>
<fx:object datafield1="数据1" datafield2="数据1" datafield3="数据1"></fx:object>
<fx:object datafield1="数据2" datafield2="数据2" datafield3="数据2"></fx:object>
<fx:object datafield1="数据3" datafield2="数据3" datafield3="数据3"></fx:object>
<fx:object datafield1="数据4" datafield2="数据4" datafield3="数据4"></fx:object>
</s:arraylist>
</s:datagrid>
</s:application>