Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery context menu disable menu items

For my asp.net mvc 3 application I'm using this plugin. But I have a problem, I want to enable or disable some menu items, after I have created the menu, like:

    <script type="text/javascript">
    $(function(){
        /**************************************************
         * Menu 1
         **************************************************/
        $.contextMenu({selector: '.context-menu-one', items: {
            edit: {name: "Edit", icon: "edit", callback: $.noop, accesskey:"e d i t"},
            cut: {name: "Cut", icon: "cut", callback: $.noop, accesskey:"c u t"},
            copy: {name: "Copy", icon: "copy", callback: $.noop, accesskey:"c o p y"},
            paste: {name: "Paste", icon: "paste", callback: $.noop, accesskey:"p a s t e"},
            "delete": {name: "Delete", icon: "delete", callback: $.noop, accesskey:"d e l t"},
            sep1: "---------",
            quit: {name: "Quit", icon: "quit", callback: $.noop, accesskey:"q u i t"}
        }});

        //not working
        $('.context-menu-one').contextMenu('commands[0].disabled','true');
      });

  </script> 

Not working, any Idea?

like image 407
iknowitwasyoufredo Avatar asked Oct 13 '11 09:10

iknowitwasyoufredo


1 Answers

According to the plugin's documentation, you can specify a function to be called in order to determine if a menu item is disabled or not.

So, you can have that function close over a local variable, and update that variable in order to enable or disable the items. Something like:

$(function() {
    var itemsDisabled = {};  // Enable everything initially.
    $.contextMenu({
        selector: ".context-menu-one",
        items: {
            // [...]
            cut: {
                name: "Cut",
                icon: "cut",
                callback: $.noop,
                accesskey: "c u t",
                disabled: function(key, opt) {
                    return !!itemsDisabled[key];
                }
            }
            // [...]
        }
    });

    // Disable the "Cut" menu item.
    itemsDisabled["cut"] = true;
});
like image 174
Frédéric Hamidi Avatar answered Nov 10 '22 05:11

Frédéric Hamidi