DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Building a RESTful Service Using ASP.NET Core and dotConnect for PostgreSQL
  • Working With dotConnect for Oracle in ASP.NET Core
  • Implementing Cache Dependency in ASP.NET Core
  • Working With dotConnect for SQL Server in ASP.NET Core

Trending

  • Exploring Different Continuous Integration Servers: Streamlining Software Development
  • Can Redis Be Used as a Relational Database?
  • Protect Your Keys: Lessons from the Azure Key Breach
  • Agile Frameworks in Action: Enhancing Flexibility in Service Delivery
  1. DZone
  2. Coding
  3. Frameworks
  4. ASP.NET MVC: Using jQuery context menu with tables

ASP.NET MVC: Using jQuery context menu with tables

Gunnar Peipman user avatar by
Gunnar Peipman
·
May. 26, 10 · News
Like (0)
Save
Tweet
Share
8.58K Views

Join the DZone community and get the full member experience.

Join For Free

I needed to add context menus to some tables of my intranet application. After trying some components I found one that does everything I need and has no overhead. In this posting I will show you how to use jQuery context menu plug-in and how to attach it to tables.

I found context menu plug-in by Chris Domigan and it was very easy to integrate to my application (when comparing some other plug-ins that work only on demo pages and in simple scenarios). Thanks, Chris, for great work! Now let’s use this context menu plug-in with table.

Before we go on let’s see what we are trying to achieve. The following screenshot fragment shows simple context menu that we want to attach to our table.


And when we click some menu option then something should happen too. :)

Installing context menu plug-in

  1. Download plug-in (if download link is broken then open demo page and I think you know how to get plug-in from there).
  2. Copy jquery.contextmenu.js to your scripts folder.
  3. Include it in your masterpage or in the page where you plan to use context menus.
  4. Make sure plug-in is included correctly (use Firebug or some other tool you like).
  5. Save the page.

Defining context menu

Now let’s define context menu. Here is fragment on context menu definition from my code.

<div class="contextMenu" id="myMenu1">

<ul>

<li id="email"><img src="/img/e-mail.png" />E-mail</li>

<li id="homepage"><img src="/img/homepage.png" />Homepage</li>

</ul>

</div>

div with id myMenu1 is container of context menu. Unordered list inside container defines items in context menu – simple and elegant!

Adding context menu to table

I have table with persons. It is simple HTML. I omitted commands column from this and the next table to keep them simple and more easily readable.


<table>

<tr>

<th>Name</th>

<th>Short</th>

<th>Address</th>

<th>Mobile</th>

<th>E-mail</th>

</tr>

<% foreach(var person in Model.Results) { %>

<tr>

<td><%=person.FullName %></td>

<td><%=person.ShortName %></td>

<td><%=person.FullAddress %></td>

<td><%=person.Mobile %></td>

<td><%=person.Email %></td>

</tr>

<% } %>

</table>

To get context menu linked to table rows first cells we need to specify class for cells and ID. We need ID because we have to know later which ID has the row on which user selected something from context menu.

<table>

<tr>

<th>Name</th>

<th>Short</th>

<th>Address</th>

<th>Mobile</th>

<th>E-mail</th>

</tr>

<% foreach(var person in Model.Results) { %>

<tr>

<td class="showContext" id="<%= person.Id %>"><%=person.FullName %></td>

<td><%=person.ShortName %></td>

<td><%=person.FullAddress %></td>

<td><%=person.Mobile %></td>

<td><%=person.Email %></td>

</tr>

<% } %>

</table>

Now we have only one thing to do – we have to write some code that attaches context menu to table cells.

Catching context menu events

Now we will make everything work. Relax, it is only couple of lines of code, thank to jQuery.


<script type="text/javascript">

$(document).ready(function () {

$('td.showContext').contextMenu('myMenu1', {



bindings: {

'email': function (t) {

document.location.href = '/contact/sendmail/' + t.id;

},

'homepage': function (t) {

document.location.href = '/contact/homepage/' + t.id;

}

}

});

});

</script>

I think that first lines doesn’t need any comments. Take a look at bindings. We gave ID to table cells because it is carried also to bound events. We can use also more complex ID-s if we have more than one table with context menus on our form.

Now we are done. Save all files, compile solution, run it and try out how context menu works.

Conclusion

We saw than using jQuery with context menu component allows us easily create powerful context menus for our user interfaces. Context menu was very easy to define. We were also able to attach context menu to table and use ID of current row entity also in events of context menu. To achieve this we needed only some minor modifications in view and couple of lines of JavaScript.

Database Context menu ASP.NET JQuery

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a RESTful Service Using ASP.NET Core and dotConnect for PostgreSQL
  • Working With dotConnect for Oracle in ASP.NET Core
  • Implementing Cache Dependency in ASP.NET Core
  • Working With dotConnect for SQL Server in ASP.NET Core

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: