Jstree drag confirmation

I developed a warning warning that appears  do you want to move this file or folder ?

if I click no: it will return to the default folder.

if yesit goes to a new folder.

So my problem is when I drag the parent folder , a confirmation message appears and, if I click no, the movement does not stop, this means that the folder will be moved to a new folder

For example, I have:

  • root1
    • folder1
      • Child1
  • root2

In my jstree, I have the nodes Root1 and Root2 , which are the parent nodes, so when I move child1 to Root2, it works fine. but when I move ROOT2 to ROOT1, it did not stop moving when I pressno

This is my code:

// html demo
$('#tree').jstree({
"core" : {
"animation" : 0,
"check_callback" : true,
"data" : [
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
{ "id" : "ajson11", "parent" : "#", "text" : "Simple root 1 node" },
{ "id" : "ajson12", "parent" : "#", "text" : "Simple root 2 node" },
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1","type" : "file" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2","type" : "file" },
]
},
  "types" : {
    "#" : {
      "max_children" : 1, 
      "max_depth" : 4, 
      "valid_children" : ["root"]
    },
    "root" : {
      "icon" : "/static/3.1.1/assets/images/tree_icon.png",
      "valid_children" : ["default"]
    },
    "default" : {
      "valid_children" : ["default","file"]
    },
    "file" : {
      "icon" : "glyphicon glyphicon-file",
      "valid_children" : []
    }
  },
"plugins" : [
"contextmenu", "dnd", "types"
]
});


var Parent = 0;
var newParent = 0;
var Pos = 0;
var newPos = 0;

$(document).on('dnd_start.vakata', function (event, data) {
    sel = "li#"+ data.data.nodes[0] +".jstree-node";
    Parent = $('#tree').jstree(true).get_node(data.data.nodes[0]).parent;
    Pos = $(sel).index();
});

$(document).on('dnd_stop.vakata', function (event, data) {
    node = data.data.origin.get_node(data.data.nodes[0]);
    if (node.type == "root")
    {
        return false;
    }

    if (confirm("Voulez vous vraiment deplacer le fichier ou le dossier ?") === false)
    {
        $('#tree').jstree(true).move_node(node,Parent,Pos);
        return false;
    }
    sel = "li#"+ data.data.nodes[0] +".jstree-node";
    newPos =  $(sel).index();
    newParent = node.parent; 
});

and this is an example

+4
source share
1 answer

No need to use events dnd_*, you will be better off using the function check_callback:

"check_callback" :  function (op, node, par, pos, more) {
    if ((op === "move_node" || op === "copy_node") && node.type && node.type == "root") {
        return false;
    }
    if ((op === "move_node" || op === "copy_node") && more && more.core && !confirm('Are you sure ...')) {
        return false;
    }
    return true;
},

Here is your updated demo: http://jsfiddle.net/bq8xme0y/6/

You can also use the function dnd.is_draggable- to prevent dragging and dropping certain nodes (it seems to me that this is something else you need).

+7
source

Source: https://habr.com/ru/post/1589415/


All Articles