6

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?

1 Answer 1

15

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;
});
Sign up to request clarification or add additional context in comments.

2 Comments

BTW, I have found out that in order to hide an item, and not just disable it, you can use the "visible" option instead of "disabled". The definition is done the same way, except for the fact that the function under "visible" runs twice and in the first run, the parameters are not returned. So you will have to check first if the "$trigger" returns anything and then run the needed code in this function.
@TheCuBeMan it might have been nice if they mentioned that in the documentation! swisnl.github.io/jQuery-contextMenu/docs/items.html#visible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.