Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jstree move, drag and drop

Tags:

jstree

I want to implement the move functionality for a node in jstree. Is it the move that needs to be implemented or the drag and drop? Also, it would be nice to have working code for binding the container to event and the event code.

like image 998
Len Smith Avatar asked May 07 '12 17:05

Len Smith


People also ask

How do you implement drag and drop in jsTree?

To implement rules, you can use core. check_callback or possibly, the types plugin; the crrm plugin doesn't exist anymore. Bind to move_node. jstree to perform some action on completion of move (drop).

What is jsTree?

jsTree is jquery plugin, that provides interactive trees. It is absolutely free, open source and distributed under the MIT license. jsTree is easily extendable, themable and configurable, it supports HTML & JSON data sources and AJAX loading.


2 Answers

$("#demo1").jstree({
....
.bind("move_node.jstree", function (e, data) {

    /*
    requires crrm plugin

    .o - the node being moved
    .r - the reference node in the move
    .ot - the origin tree instance
    .rt - the reference tree instance
    .p - the position to move to (may be a string - "last", "first", etc)
    .cp - the calculated position to move to (always a number)
    .np - the new parent
    .oc - the original node (if there was a copy)
    .cy - boolen indicating if the move was a copy
    .cr - same as np, but if a root node is created this is -1
    .op - the former parent
    .or - the node that was previously in the position of the moved node */

    var nodeType = $(data.rslt.o).attr("rel");
    var parentType = $(data.rslt.np).attr("rel");

    if (nodeType && parentType) {
        // TODO!
    }
})
});
like image 88
Rikin Patel Avatar answered Sep 20 '22 05:09

Rikin Patel


The above approaches do not work with the latest versions of jstree (3.3.7 as of today).

The first line of Bojin's answer still holds true. To implement rules, you can use core.check_callback or possibly, the types plugin; the crrm plugin doesn't exist anymore. Bind to move_node.jstree to perform some action on completion of move (drop). By default, the dnd plugin allows re-ordering (dropping between two nodes) and copying (Ctrl + drag), in addition to moving a node. The code snippet below shows how to disable these additional behaviors.

$('#treeElement').jstree({
    'core': {
        'check_callback': CheckOperation,
        ...
    },
    'plugins': ['dnd']
})
.bind("move_node.jstree", function(e, data) {
    //data.node was dragged and dropped on data.parent
});

function CheckOperation(operation, node, parent, position, more) {
    if (operation == "move_node") {
        if (more && more.dnd && more.pos !== "i") { // disallow re-ordering
            return false;
        }
        ... more rules if needed ...
        else {
            return true;
        }
    }
    else if (operation == "copy_node") {
        return false;
    }
    return true;
}
like image 44
djm Avatar answered Sep 23 '22 05:09

djm