jstree remove default elements from context menu

LostMohican picture LostMohican · Dec 13, 2011 · Viewed 11.3k times · Source

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

Answer

contrebis picture contrebis · Dec 14, 2011

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.