Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jstree remove default elements from context menu

I have a problem with JsTree's contextmenu, how can I remove the default elements from the contextmenu like Create, Delete, Rename? I want to provide elements of my own, but the default elements are still at the contextmenu.

    "contextmenu" : {
                    "items" : {
                        "IsimVer" : {
                            "label" : "İsim Değiştir",
                            "action" : function (obj) { this.rename(obj); }
                        },
                        "Ekle" : {
                            "label" : "Ekle",
                            "action" : function (obj) { this.create(obj); }
                        },
                        "Sil" : {
                            "label" : "Sil",
                            "action" : function (obj) { this.remove(obj); }
                        }
}
like image 482
LostMohican Avatar asked Dec 13 '11 15:12

LostMohican


2 Answers

I had this issue a couple of days ago but haven't yet decided if this is a bug or a feature. It may be related to the order in which the plugins are loaded.

What worked for me was returning the items from a function:

"contextmenu" : {
    "items" : function ($node) {
        return {
            "IsimVer" : {
                "label" : "İsim Değiştir",
                "action" : function (obj) { this.rename(obj); }
            },
            "Ekle" : {
                "label" : "Ekle",
                "action" : function (obj) { this.create(obj); }
            },
            "Sil" : {
                "label" : "Sil",
                "action" : function (obj) { this.remove(obj); }
            }
        };
    }
}

After some searching it seems that the default behaviour is for your menu items to extend the defaults, so this is a feature. Unfortunately the documentation currently lacks the detail on this point.

like image 144
contrebis Avatar answered Nov 02 '22 19:11

contrebis


If you like to modify labels of existing items or remove a few, a simple solution like below will work

"contextmenu": {
   "items": function(node) {
           var defaultItems = $.jstree.defaults.contextmenu.items();
           defaultItems.create.label = "Ekle";
           delete defaultItems.ccp;
           return defaultItems;
        }
    }

This will set "Create" items label as "Ekle" and remove cut copy paste from default items.

like image 36
Gorky Avatar answered Nov 02 '22 20:11

Gorky