4

I have a single jsTree and I want some of its nodes to be moveable. The root node and its immediate children cannot be moved.

I am using crrm and it works as expected but it still allows me to drag all nodes, even those nodes (immediately below root) that cannot be dropped anywhere. I don't want them to be draggable at all i.e. user should not be able to pick them up at all.

Ali
  • 1,462
  • 2
  • 17
  • 32

3 Answers3

16

In the current version of jsTree (3.0.8) you do this:

$('#my_jstree')
            .jstree({
                "core" : {
                    "check_callback" : true
                },
                "dnd" : {
                    "is_draggable" : function(node) {
                        console.log('is_draggable called: ', node[0]);
                        if (node[0].type != 'MY-DRAGGABLE-TYPE') {
                            alert('this type is not draggable');
                            return false;
                        }
                        return true;
                    }
                },
                "types" : {
                    "default" : {
                        "icon" : "glyphicon glyphicon-flash"
                    },
                    "MY-DRAGGABLE-TYPE" : {
                        "icon" : "glyphicon glyphicon-info-sign"
                    }
                },
                "plugins" : [ "dnd", "types" ]})

Use the is_draggable function along with the types plugin to control what can be dragged around. If you want to see the icons I used include bootstrap's glyphicons stylesheet. The code here is assuming you already have a div and list for jstree in your HTML. I'll post a plunkr if anyone can't understand the code above.

EDIT - to prevent dropping do this:

"core" : {
                    "check_callback" : function(operation, node, node_parent, node_position, more) {
                        if (operation == 'move_node') {
                            console.log(node_parent);
                            if (node_parent.type == 'MY-DROPPABLE-TYPE') {
                                return true
                            } else
                                return false;
                        }
                    }
                },

Use the check_callback function to screen the droppable's type. Use the node_parent to get the droppable.

cmzmuffin
  • 181
  • 1
  • 5
0

Similar question asked, check out my answer: dnd, how to restrict dropping to certain node types?

There needs to be some type of smell test on the crrm.move.check_move function. Your your case this would be the root and first children.

Community
  • 1
  • 1
MMeah
  • 1,032
  • 1
  • 9
  • 18
  • Similar but not what I am asking here. crrm check_move is not useful for my problem. It only stops me from dropping a dragged node. I don't want the user to be able to drag nodes that cannot be dropped anywhere. – Ali Jun 26 '12 at 08:44
0

I think what you need is drag_check

 "dnd"  :   {

        "drop_target"   :   false,


        "drag_check"    :   function(data)

        {  
          if( data.r.attr("id") ==  ... ) 
                    return false;
           //if you want to enable drag for certain nodes  return following
           return {
                    after : true,
                    before : false,
                    inside : false
                }
          }
Dipesh KC
  • 3,195
  • 3
  • 32
  • 50
  • I believe the "dnd" callbacks are only when dragging and dropping external object. The question was about node internal to the tree. – Marc Rochkind Sep 28 '13 at 19:09