0

I want to create programmatically a new node. Then I want to add this before another node as sibling.

This code does not work:

var node = $("#TreeDiv").dynatree("getActiveNode");
var nextSiblingNode = node.getNextSibling();
var childNode = node.addChild({ title: response.title, key: response.unitId }, nextSiblingNode);

What do I wrong?

UPDATE:

Thats the exception I got with the above code:

Unhandled exception at line 4, column 20662 in http://localhost:1726/Scripts/jquery.dynatree.min.js

0x800a139e - runtime error in JavaScript: <beforeNode> must be a child of <this>
Elisabeth
  • 20,496
  • 52
  • 200
  • 321

1 Answers1

1

After creating the node as a child I had to move the node and it worked... of course the code still needs some null checks ;-)

var newNode = { title: response.title, key: response.unitId };
var activeNode = $("#TreeDiv").dynatree("getActiveNode");
var nextSiblingNode = activeNode.getNextSibling();
if (nextSiblingNode != null) {
    newNode = activeNode.addChild(newNode);
    newNode.move(nextSiblingNode, 'before');
}
else
{                      
    var parentNode = activeNode.getParent();
    newNode = parentNode.addChild(newNode);
}
newNode.activate(true);
newNode.focus(true);
Elisabeth
  • 20,496
  • 52
  • 200
  • 321