2012-11-24 137 views
0

我想以編程方式創建新節點。 然後我想在另一個節點之前添加它作爲兄弟。創建一個節點並將其作爲兄弟節點添加到另一個與dynatree的兄弟之前

此代碼不起作用:

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

什麼我錯了嗎?

UPDATE:

,這是我與上面的代碼得到了異常:

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> 
+0

你得到什麼錯誤信息? – ajtrichards

+0

我用錯誤消息更新了我的問題。 – Elisabeth

+0

@ socialrel8關於錯誤消息的任何想法? – Elisabeth

回答

1

創建該節點作爲一個孩子,我不得不搬到節點後和它的工作......當然的代碼仍然需要一些空的檢查;-)

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); 
相關問題