如何使用Jquery contextMenu X2更新contextMenu

时间:2020-04-21 18:06:08

标签: jquery contextmenu

我已经像这样添加了contextMenu:

buildContentMenu = {

   commands : {}, // i'm add items from external function

   makeMenu: function makeMenu(){

     $.contextMenu({ 
        selector: '.fullRegularRow',
        items: this.commands,
        events: {
            show : function(options){
                 var serverId = this.attr("id").replace("server","").replace(/\-+/g, '.');
                 var fanMode = this.attr("fanMode");
                 var powerMode = this.attr("powerMode");
                 menuListManager.addOptionsToMenu(serverId, fanMode, powerMode)
                 console.log(serverId);

            }
        }        
    });

现在我想将其添加到菜单列表中。

所以我更新了commands对象,并尝试像这样更新contectMenu但对我不起作用:

    $.contextMenu('update');

任何人都可以帮助我如何更新菜单?

1 个答案:

答案 0 :(得分:0)

几个小时后,我找到了答案。

要点是,如果我想在显示菜单之前构建菜单,则必须使用 build 。 现在,在准备显示菜单之前,我可以通过编辑“对象命令”来重新排列我要显示的内容。

这是我的完整代码:

        $.contextMenu({ 
        selector: '.fullRegularRow, .COLservers4, .COLservers2, .quarterOf2U, .fullDoubledRow, .fullQuadrdRow',
            build : function($triggerElement, e){
                var fanmode = ( $triggerElement[0].attributes["data-fanmode"].value);
                var powermode = ( $triggerElement[0].attributes["data-powermode"].value);

                if(fanmode == "STANDARD_MODE"){
                    menuListManager.commands["strdFanMode"] = {callback: function(key, opt){ menuList.sendMenuRequest(key, serverURL); }, name: "Fan mode", icon: "fas fa-check"};
                    menuListManager.commands["zutaFanMode"] = {callback: function(key, opt){ menuList.sendMenuRequest(key, serverURL); }, name: "Fan mode"};
                }else{
                    menuListManager.commands["strdFanMode"] = {callback: function(key, opt){ menuList.sendMenuRequest(key, serverURL); }, name: "Fan mode"};
                    menuListManager.commands["zutaFanMode"] = {callback: function(key, opt){ menuList.sendMenuRequest(key, serverURL); }, name: "Fan mode", icon: "fas fa-check"}; 
                }

                if(powermode == "STANDARD_MODE"){
                    menuListManager.commands["strdPowerMode"] = {callback: function(key, opt){ menuList.sendMenuRequest(key, serverURL); }, name: "Power mode", icon: "fas fa-check"};
                    menuListManager.commands["zutaPowerMode"] = {callback: function(key, opt){ menuList.sendMenuRequest(key, serverURL); }, name: "Power mode"};
                }else{
                    menuListManager.commands["strdPowerMode"] = {callback: function(key, opt){ menuList.sendMenuRequest(key, serverURL); }, name: "Power mode"};
                    menuListManager.commands["zutaPowerMode"] = {callback: function(key, opt){ menuList.sendMenuRequest(key, serverURL); }, name: "Power mode", icon: "fas fa-check"}; 
                }

                return {
                    callback: function(){},
                    items: menuListManager.commands,      
                };

            }
    });

希望它将对某人有所帮助。