Right or Left Click Context Menu Using jQuery Demo

by Bill Beckelman 4. November 2008 05:30

In my seemingly never ending quest to find the coolest jQuery plugins I came across the jQuery Context Menu Plugin by Cory S.N. LaViska. As you have probably figured out, I use a lot of ASP.NET ListViews so I generally tryout the plugins I find using a simple ListView which in this case basically translates to a standard html table.

According to Cory's site, the plugin "features easy implementation, keyboard shortcuts, CSS styling, and control methods." I would say he definitely delivers on all four. One thing I wanted to be able to do though that I didn't find baked in was to call the menu with a left click instead of the plugin's standard right click so I could cater to the user who might not know about right clicking. This was easy enough to change though with just a few lines in the plugin to include an override to look for a left click.

Be sure to checkout Cory's plugin page for detailed usage and more examples.

Demo | Download

Screenshot of End Product:

image

    Features:

    • Menu will appear whenever one of the table rows is right clicked.

    • Menu will appear whenever one of the cells containing a lightning icon is left clicked.


    HTML Markup for the Menu:

    <!-- Right Click Menu -->
    <ul id="myMenu" class="contextMenu">
        <li class="insert"><a href="#insert">Add New</a></li>        
        <li class="edit"><a href="#edit">Edit</a></li>                    
        <li class="delete"><a href="#delete">Delete</a></li>            
    </ul>

    Changes to Plugin to Allow Left Clicking:

    I added "if (o.leftButton == undefined) o.leftButton = false;" to the defaults section of the plugin and then where it checks for which button was clicked I changed
    the code from "if(evt.button==2) {" to "if (evt.button == 2 || o.leftButton == true) {." Then when I setup the menu in the ready function I overrode the default by setting leftButton: true.

    jQuery On My Page:

    $(document).ready(function() {
        //Tie a menu to the right click of each row of the table with a class of customerRow
        $(".customerRow").contextMenu({ menu: 'myMenu' }, 
            function(action, el, pos) { contextMenuWork(action, el, pos); });
    
        //Tie a menu the left click of each td with the class of openmenu !Important - requires modified
        //jquery.contextMenu.js that allows for leftButton: true
        $(".openmenu").contextMenu({ menu: 'myMenu', leftButton: true }, 
            function(action, el, pos) { contextMenuWork(action, el.parent("tr"), pos); });
    
        });
    
        function contextMenuWork(action, el, pos) {
    
            switch (action) {
                case "delete":
                    {
                        //Popup Delete Confirmation - included in demo and in download
                        break;
                    }
                case "insert":
                    {
                        //Popup Insert Dialog- included in demo and in download
                        break;
                    }
    
                case "edit":
                    {
                        //Popup Edit Dialog
                        break;
                    }
            }
       }
    });    

    Be sure to checkout the demo and download for the complete code and refer to Cory's plugin for additional usage details. Let me know in the comments what you think, what I messed up and what could be improved.

    [dzone]    kick it on DotNetKicks.com

    Tags:

    ASP.NET | jQuery

    Comments

    Add comment


    (Will show your Gravatar icon)

      Country flag

    biuquote
    • Comment
    • Preview
    Loading



    Powered by BlogEngine.NET 1.4.5.7
    Theme by Mads Kristensen


    About Me

    I live and work in Salt Lake City, Utah. My background is in aviation. I have a degree in Aeronautical Science from Embry-Riddle Aeronautical University in Prescott, AZ. I have worked as a commercial airline pilot and most recently as a technical advisor for a charter airline.