JQuery right-click menu contextMenu to use an instance

Source: Internet
Author: User

In the following scenario, we will design a table grid that needs to be right-clicked, added, deleted, and saved on each row. Grid I used the gridview, and I used the style provided by Microsoft. I didn't need to make a good look at the test cases. I was not an artist anymore. Haha, I was so lazy.

First, some comrades say that the truth is true only when there is a picture:

Ui code:Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default2.aspx. cs" Inherits = "Default2" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<% -- %>
<Script src = "Script/jquery. js" type = "text/javascript"> </script>
<Script src = "Script/jquery. contextmenu. r2.js" type = "text/javascript"> </script>
<Style type = "text/css">
. SelectedRow
{
Background: yellow;
}
. ContextMenu
{
Display: none;
}
</Style>
<Script type = "text/javascript">
$ (Function (){
$ ('# GridView1 tr: gt (0)'). contextMenu ('menu ',
{
Bindings:
{
'Add': function (t, target ){
Alert ('triggers: '+ t. id +' Add '+ "taget by:" + $ ("td: eq (0)", target). text ());
},
'Delete': function (t, target ){
Alert ('triggers: '+ t. id + 'delete' + "taget by:" + $ ("td: eq (0)", target). text ());
$ (Target). remove ();
},
'Save': function (t, target ){
Alert ('triggers: '+ t. id +' Save '+ "taget by:" + $ ("td: eq (0)", target). text ());
},
'About': function (t, target ){
Alert ('Code by http://www.cnblogs.com/whitewolf /');
}
},
OnShowMenu: function (e, menu ){
If (parseInt ($ ("td: eq (0)", e. currentTarget). text ()> 10 ){
$ ("# Save", menu). remove ();
}
$ (E. currentTarget). siblings (). removeClass ("SelectedRow"). end (). addClass ("SelectedRow ");
Return menu;
}
});
})
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Div class = "contextMenu" id = "menu">
<Ul>
<Li id = "add">

Edit </li>
<Li id = "delete">

Delete </li>
<Li id = "save">

Save </li>
<Li id = "About">

About </li>
</Ul>
</Div>
<Asp: GridView ID = "GridView1" runat = "server" Width = "100%" BackColor = "White" BorderColor = "# CCCCCC"
BorderStyle = "None" BorderWidth = "1px" CellPadding = "3">
<RowStyle ForeColor = "#000066" type = "regxph" text = "yourobjectname"/>
<FooterStyle BackColor = "White" ForeColor = "#000066"/>
<PagerStyle BackColor = "White" ForeColor = "#000066" HorizontalAlign = "Left"/>
<SelectedRowStyle BackColor = "#669999" Font-Bold = "True" ForeColor = "White"/>
<HeaderStyle BackColor = "#006699" Font-Bold = "True" ForeColor = "White"/>
</Asp: GridView>
</Div>
</Form>
</Body>
</Html>

Note:
1: For the contextMenu, we hide some menu items based on data records. This can be used in the onShowMenu event, according
E. currentTarget is used to obtain data from the trigger source. In the "remove" menu. For example, in a test case, if id> 10, the data cannot be saved.
If (parseInt ($ ("td: eq (0)", e. currentTarget). text ()> 10 ){
$ ("# Save", menu). remove ();
}
2: event registration: Get data based on the second parameter target, and the first parameter t gets the menu item. For example:Copy codeThe Code is as follows: 'add': function (t, target ){
Alert ('triggers: '+ t. id +' Add '+ "taget by:" + $ ("td: eq (0)", target). text ());
},

Ajax is used to communicate with the server. You can use the previous component jQuery Ajax to simulate the AjaxPro. Utility. RegisterTypeForAjax auxiliary method, which will make ajax communication more simple.
There is a problem in the source code below:
Originally, currentTarget is always undefined.Copy codeThe Code is as follows: if (!! Cur. onShowMenu) menu = cur. onShowMenu (e, menu );
$. Each (cur. bindings, function (id, func ){
$ ('#' + Id, menu). bind ('click', function (e ){
Hide ();
Func (trigger, currentTarget );
});
});

After I modify:Copy codeThe Code is as follows: if (!! Cur. onShowMenu) menu = cur. onShowMenu (e, menu );
$. Each (cur. bindings, function (id, func ){
$ ('#' + Id, menu). bind ('click', function (ev ){
Hide ();
Func (trigger, e. currentTarget );
});
});

In this way, everything is normal.
There is very little content. Everything is interrupted here. It's over!
Attachment download: Demo

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.