Use parentNode(), because parent() returns the array which holds this dataItem as you noticed.
Kendo UI: TreeView - How to tell if a given node has parents
-
12-12-2021 - |
Question
I am trying to use the dragend event on a TreeView in order to send a command to the server in order to make the appropriate change, however, to do this I need parent information on both the target and destination nodes. Currently I have the following:
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
}
What I have noticed is that the parent() call returns a list and it doesn't seem to me to have any indication of the actual parent. Perhaps I am catching the wrong event, but here, the parent() function seems to return the siblings of the target node. I would also like to be able to tell if the node doesn't have a parent (ie it is at the root level)
Solution
OTHER TIPS
I don't see the parentNode() method either. To tell if a node is a top level node in the dragend event you can just use jquery. You can use jquery to grab the UID as well, which then you can use to get the data node.
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;
}