2012-12-24 43 views
1

我想在TreeView上使用dragend事件來發送一個命令到服務器以便進行相應的更改,但是,要這樣做我需要目標節點和目標節點上的父級信息。目前,我有以下幾點:Kendo UI:TreeView - 如何判斷一個給定的節點是否有父母

function dragEndEvent(e) { 

    var treeViewData = $(".hierarchy-tree").data('kendoTreeView'); 

    var quotaSetID = $("#quotaset-id").val(); 
    var columnID = $("#treeViewColumnID").val(); 
    var targetNode = treeViewData.dataItem(e.sourceNode); 
    var targetParentNode = targetNode.parent(); 
    var destinationNode = treeViewData.dataItem(e.destinationNode); 
    var destinationParentNode = null; 
    if(destinationNode!=null) 
     destinationParentNode = destinationNode.parent(); 

    var targetName = targetNode.text; 
    var targetID = targetNode.id; 
    var targetsParentID = null; 
    if (targetParentNode != null && targetParentNode.length == 1) 
     targetsParentID = targetParentNode[0].id; 

    var destinationName = null; 
    var destinationID = null; 
    var destinationsParentID = null; 
    if (destinationNode != null) { 
     destinationName = destinationNode.text; 
     destinationID = destinationNode.id; 
     if (destinationParentNode != null && destinationParentNode.length == 1) 
      destinationsParentID = destinationParentNode[0].id; 
    } 
    // Followed by ajax query 
} 

我所注意到的是,父()調用返回一個列表,它似乎並沒有對我有實際的父的任何跡象。也許我正在捕捉錯誤的事件,但在這裏,parent()函數似乎返回目標節點的兄弟節點。我也想能夠告訴我們,如果節點沒有父(即它是在根級別)

回答

3

使用parentNode(),因爲父()返回你注意到它持有該DataItem的數組。

+0

我不同意以上內容。 Kendo似乎沒有parentNode方法或屬性。 –

0

我沒有看到parentNode()方法。要判斷節點是否是dragend事件中的頂級節點,只需使用jquery。您也可以使用jquery獲取UID,然後您可以使用它來獲取數據節點。

var targetNode = e.destinationNode; 
if ($(targetNode).parent("ul").parent(".k-treeview").length === 1) { 
    //top level node 
} else { 
    //not top level node 
    var htmlNode = $(targetNode).parent("ul").parent(".k-item"); 
    //if you need the telerik version of the node 
    var treeViewData = $(".hierarchy-tree").data('kendoTreeView'); 
    var parentUid = $(htmlNode).data("uid"); 
    var parentNode = treeViewData.findByUid(parentUid); 
    var parentDataNode = treeViewData.dataItem(parentNode); 
    var parentid = parentDataNode.id; 
} 
相關問題