Copy Code code as follows:
<?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 ();
Listen for events after the right button menu pops up
Mycontextmenu.addeventlistener (Contextmenuevent.menu_select, Menuselectedhandler);
Mydatagrid.contextmenu = Mycontextmenu;
}
Delete an existing menu item
Private Function Removedefaultitems (): void
{
Mycontextmenu.hidebuiltinitems ();
var defaultitems:contextmenubuiltinitems = Mycontextmenu.builtinitems;
Defaultitems.print = false;
}
Add User custom menu items
Private Function Addcustommenuitems (): void
{
var item:contextmenuitem = new Contextmenuitem ("Delete Row");
The default menu item
Item.visible = false;
MyContextMenu.customItems.push (item);
Listen for events after selecting menu items
Item.addeventlistener (Contextmenuevent.menu_item_select, Itemselectedhandler);
}
Event handler function after pop-up menu
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;
}
}
Event handler function after selecting menu item
Private Function Itemselectedhandler (event:contextmenuevent): void
{
if (mydatagrid.selectedindex >= 0)
{
MyDataGrid.dataProvider.removeItemAt (Mydatagrid.selectedindex);
}
}
]]>
</fx:Script>
<s:datagrid requestedrowcount= "4" id= "Mydatagrid"
Width= ">"
<s:columns>
<s:ArrayList>
<s:gridcolumn datafield= "DataField1" headertext= "column 1" id= "col1" ></s:GridColumn>
<s:gridcolumn datafield= "dataField2" headertext= "column 2" ></s:GridColumn>
<s:gridcolumn datafield= "dataField3" headertext= "column 3" ></s:GridColumn>
</s:ArrayList>
</s:columns>
<s:typicalItem>
<fx:object datafield1= "Sample Data" datafield2= "Sample Data" datafield3= "Sample Data" ></fx:Object>
</s:typicalItem>
<s:ArrayList>
<fx:object datafield1= "Data 1" datafield2= "Data 1" datafield3= "Data 1" ></fx:Object>
<fx:object datafield1= "Data 2" datafield2= "Data 2" datafield3= "Data 2" ></fx:Object>
<fx:object datafield1= "Data 3" datafield2= "Data 3" datafield3= "Data 3" ></fx:Object>
<fx:object datafield1= "Data 4" datafield2= "Data 4" datafield3= "Data 4" ></fx:Object>
</s:ArrayList>
</s:DataGrid>
</s:Application>