Add the right key menu item to the DataGrid in Flex _flex

Source: Internet
Author: User
Tags xmlns
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.