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

佐手、 提交于 2019-12-19 17:38:14

问题


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

回答1:


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;



回答2:


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



回答3:


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.




回答4:


$(document).on('dnd_stop.vakata', function(e, data) {
    var inst = $.jstree.reference('#jstree');
    console.log("END DROP:");
    var sourceID = data.data.nodes[0];
    console.log("Source ID: " + sourceID);
    var targetNode = inst.get_node(data.event.target, true);
    var targetID = targetNode[0].id;
    console.log("Target ID: " + targetID);
});



回答5:


If you need to do this via the check_callback then you can access the target node via the more parameter.

'check_callback': function(operation, node, node_parent, node_position, more) {


    // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
    // in case of 'rename_node' node_position is filled with the new node name


    if (operation === "move_node") {

        if (more.ref.data.type === "folder") {

            return true;

        } 

    }

    return false; //allow all other operations

}



回答6:


To get the target node you use the dnd_stop.vakata event. Once you get the node you can access to the different properties like id:

$(document).bind("dnd_stop.vakata",function(e, data) {
    var targetNode = $('#jstree').jstree(true).get_node($(data.event.target));
    var nodeId = targetNode.id;
});



回答7:


you just need to call:

'check_callback': function(operation, node, node_parent, node_position, more) {
  // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'

  if (operation === "move_node") {
      var node = more.origin.get_node('fiche-1');
      return true;
  }

  return true; //allow all other operations
}


来源:https://stackoverflow.com/questions/22892906/jstree-v3-0-drag-and-drop-plugin-reference-target-node-upon-dropping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!