check_callback Function for use with Drag and Drop and Types Plugin

2,197 views
Skip to first unread message

lee-da

unread,
Jan 22, 2014, 12:27:37 PM1/22/14
to jst...@googlegroups.com
In version 3 it seems that checking the valid_children while dragging and dropping a node needs to be implemented by ourselves.
(Please correct me if I'm wrong.)

So, here's my solution:

$("#jstree").jstree({
   
"core": {
       
"data": <nodes with types>,
       
"check_callback": function(operation, node, parent, position)
       
{
           
switch (operation) {
               
case "move_node":
                   
return $.inArray(this.get_type(node), this.get_rules(parent).valid_children) != -1;
           
}
           
return false;
       
}
   
},
   
"types": <types containing valid_children>,
   
"plugins": ["types", "dnd"]
});


Please let me know if there's a better solution.

Thomas

Ivan Bozhanov

unread,
Jan 23, 2014, 3:34:18 AM1/23/14
to jst...@googlegroups.com
Checks are done by jstree, no need to do it manually. The check is in jstree.types.js, line 114.

Best regards,
Ivan

Ivan Bozhanov

unread,
Jan 23, 2014, 3:36:14 AM1/23/14
to jst...@googlegroups.com
You can see this in action on the demo page - you will not be able to drop the file node inside a root node.


22 януари 2014, сряда, 19:27:37 UTC+2, Thomas Lieder написа:

Thomas Lieder

unread,
Jan 23, 2014, 4:22:12 AM1/23/14
to jst...@googlegroups.com
Ok, thanks for pointing this out.

My mistake:
Since I got used to v1 I expected a red X while dragging a node over a "forbidden" drop node - now it is still green, because the parent of the drop node allows to drop it before or after the "forbidden" drop node (note: position of the drop marker - above, below, but not centered). A bit confusing in this case might be that the "forbidden" drop node is highlighted (which is normal -> hovering). Another mistake was not to define valid children for the "#" - I started at the visible node level.

Again, thanks.
It now works as expected.

Thomas
Reply all
Reply to author
Forward
0 new messages