Limit drag and drop jstree node before and after root

In my jstree, I have a Root node, which is the parent node for everyone. I used the dnd plugin. I want to allow drag and drop anywhere in the tree, but only inside the root, that is, not earlier or after Root.

- [Root] - Node 1 - Node 1.1 - Node 1.2 + Node 2 - Node 3 + Node 3.1 

After checking the forums, I found that the drag_check event drag_check intended only for foreign nodes, and not for any node inside the tree. To check for the same node tree, we need to use the crrm -> check_move . This is where I need help. I want to return false if the node is discarded before or after [Root] .

This is where the fiddle begins - http://jsfiddle.net/juyMR/23/

+7
source share
4 answers

You are very close, you just need an else expression to return true

http://jsfiddle.net/blowsie/juyMR/59/

  "crrm" : { "move" : { "check_move" : function (data) { // alert(data.r.attr("id")); if(data.r.attr("id") == "999") { return false; } else { return true; } } } }, 
+3
source

There is a simpler solution in the current version of jstree (3.0.1), all you have to use is

 "types" : { "#" : { "max_children" : 1 } }, 

If you use a type plugin, there are two predefined types. One of them is the "#" used above. This is automatically used for the "real" root element of the tree, which means the one that is used internally. Therefore, if you add your own root as the first node without any additional configuration, everything will work fine.

+7
source

Take a look at data.o (source node) and data.np (node ​​parent) and check their identifiers in crrm check_move. Looking at an example script, don't let data.np.attr ("id") equal "jsTreeDiv".

+1
source

This can be processed out of the box without any additional features:

 $("#tree").jstree({ "types" : { "valid_children" : [ "root" ], //<== THIS IS THE IMPORTANT ONE ! "types" : { "default" : { "valid_children" : "none" }, "root" : { "valid_children" : "default" } } } }); 

You can specify "valid_children" inside each of your node types, but also one level higher (check the code above, where it says "IT IS IMPORTANT ONE".

This globally will only allow your ROOT node to have children, which means that you cannot pull anything from the root, but everywhere inside the root tree.

+1
source

All Articles