views:

321

answers:

1

I am using jsTree which is very nice. When dragging and dropping, I don't really care for the before and after types, I only want to use inside. Meaning, I am only concerned about that parent that a child is dropped into, rather than where the order is with other elements INSIDE the parent.

So, I wanted to build my callback, so it always refers to the parent node that it is inside. But, it is not fool proof, yet.

onmove : function (NODE,REF_NODE,TYPE,TREE_OBJ,RB) {
    if (TYPE == 'inside') {
        alert('Item to move:'+$(NODE).attr('rel')+' to '+$(REF_NODE).attr('rel')+' '+TYPE);
    } else if (TYPE == 'after') {
        alert('Item to move:'+$(NODE).attr('rel')+' to '+$(REF_NODE).parent().parent('li').attr('rel')+' '+TYPE);
    }
},

Does anyone have suggestions, how I can change my callback, so that the REF_NODE is always the parent that the NODE is moved into? Rather than a sibling of, which is a child of the parent?

Here is an example on jsbin: http://jsbin.com/uroye3/2

When trying the example, drag other folders onto the Templates folder. You SHOULD be alerted that you are moving the other folders into folder 4. But, if you try different before and after drags into the Templates folder, you sometimes get undefined. So, the results are not always as expected. Like I said, I just always want to know the parent folder, in this case 4, that another folder is dropped into.

+1  A: 

Taken from the jstree FAQ, this may be of help to you:

When moving or creating node “inside” another node, the callback REF_NODE does not point to the new parent node. What to do?

This is not a bug! Because you can create nodes at a specific position among the parent node’s children, passing only the parent node will not be enough. This is why you must always check the TYPE argument passed to the callbacks! If you want to always work with the parent node – add this line at the top of the callbacks:

if(TYPE != "inside") { 
  TYPE="inside"; 
  REF_NODE = $(NODE).parents("li:eq(0)").get(0); 
}
Richard