Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsTree v3.0 drag and drop plugin. Reference target node upon dropping

I use drag and drop plugin of jsTree library (ver. 3.0) With the following code I can bind to the end of drag'n'drop action, but I can not see a way to get the reference to the target node (the node I'm dropping on).

$(document).on('dnd_stop.vakata', function(e, data) {
   // how to get target_node here?
});
like image 697
eagor Avatar asked Apr 06 '14 10:04

eagor


3 Answers

I had same problem. I found other solution than event dnd_stop.vakata, which returns old data before changed position.

This works:

$('#jstree_demo_div').on("move_node.jstree", function (e, data) {
   //data.node, data.parent, data.old_parent is what you need

   //console.log(data);
   alert(data.node.id);
   alert(data.parent);
});
like image 141
kuba1999 Avatar answered Sep 28 '22 16:09

kuba1999


Another solution is to use the get_node() function on the jstree object.

$(document).on('dnd_stop.vakata', function (e, data) {
    ref = $('#jstree').jstree(true);
    parents = ref.get_node(data.element).parent;
});

You can get all parents with:

    all_parents = ref.get_node(data.element).parents;
like image 21
stacked Avatar answered Sep 28 '22 18:09

stacked


I had the same problem and had to get the ID within the dnd_stop event, so I came up with this:

$(document).on('dnd_stop.vakata', function(e, data) {
  var t = $(data.event.target);
  var targetnode = t.closest('.jstree-node');
  var nodeID = targetnode.attr("id");
});

That way I can get the ID of the targetnode, for example.

like image 33
ralondo Avatar answered Sep 28 '22 18:09

ralondo