jsTree is a great jQuery plugin for easily creating expandable trees, e.g. from a list of items. We use it in our personalised portal page for Weblearn – see Personalising WebLearn (Sakai) – to display document trees.
We have been working on increasingly the functionality of this portal page to allow students to easily upload files. To do this, we are making use of the ‘contextmenu’ functionality of jsTree, which allows you to specify a menu of actions that can be accessed by right clicking on a tree node.
One thing that wasn’t immediately obvious from the jsTree docs was how to show different actions depending on the type of node, e.g. file or folder, on which the user clicked. However, it turns out that this can be done quite easily, by passing the ‘items’ parameter of the contextmenu configuration a function (that returns an object), rather than an object, as follows:
$('#tree_container').jstree({ "plugins" : [ "contextmenu" ], "contextmenu" : { "items" : customMenu } });
You then need to set up the customMenu function to return an items object, for example:
function customMenu(node) { //Show a different label for renaming files and folders if ($(node).hasClass("jstree-closed") || $(node).hasClass("jstree-open")) { //If node is a folder var renameLabel = "Rename Folder"; } else { var renameLabel = "Rename File"; } var items = { "upload" : { "label" : "Upload File", "action" : function () { ... } }, "rename" : { "label" : renameLabel, //Different label (defined above) will be shown depending on node type "action" : function () { ... } }, "delete" : { "label" : "Delete File", "action" : function () { ... } } }; //If node is a folder do not show the "delete" menu item if ($(node).hasClass("jstree-closed") || $(node).hasClass("jstree-open")) { delete items.remove; } return items; }
You are an angel for me. I have wasted my whole night in this problem. m newbie :(… thx for the help…Bookmarking it.
thanks man, this really helped me
Awesome Solution.
I like this one line
“delete items.remove;”
Thanks baddy.