开发者

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?


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;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜