This is a migrated thread and some comments may be shown as answers.

Getting the parent node

3 Answers 2581 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Randy Hompesch
Top achievements
Rank 1
Randy Hompesch asked on 13 Aug 2013, 12:21 PM
Hi, 
I must be missing something.
if I have a treeview and I have a select event handler bound to it, I can inspect the currently selected node's text by saying e.node.text(). However, if I try to get the text of the parent node, i.e. e.node.parent().text(), I get a string with all the node's text concatenated together. All I want is a simple node.getParent function that returns the exact same object as e.node only it's parent. If the node has no parent (i.e. it s a root), then it should return null. This seems harder than it should be.
Is there a way to do this or am I missing something?
Thanks ... Ed

3 Answers, 1 is accepted

Sort by
0
Alex Gyoshev
Telerik team
answered on 14 Aug 2013, 07:16 AM
Hello Randy,

You can use the treeview.text method in order to get the text of a node, and the treeview.parent method to get the parent node.

The harder way of getting the text is caused by the rendering of the treeview -- since the parent method returns a list item (which is the actual item), calling the jQuery .text() method will return the text of all nested elements, too. If you want to stick to jQuery, get the text of the first k-in element.

Regards,
Alex Gyoshev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Randy Hompesch
Top achievements
Rank 1
answered on 14 Aug 2013, 07:39 PM
This seems highly unreliable to me.
What if there are two (or more) nodes with the same text on different branches of the tree?
I need to be sure that I'm getting the correct parent.
Using var parent = treeview.parent(treeview.findByText("bar"));
seems risky to me.
Am I missing something?
Thanks ... Ed

0
Alex Gyoshev
Telerik team
answered on 15 Aug 2013, 07:52 AM
Hello Randy,

Since you already have the node in the event handler arguments, you can use it (instead of searching the treeview):

function onSelect(e) {
     var parent = this.parent(e.node); // `this` refers to the treeview object
     var parentText = this.text(parent);
}

Regards,
Alex Gyoshev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
TreeView
Asked by
Randy Hompesch
Top achievements
Rank 1
Answers by
Alex Gyoshev
Telerik team
Randy Hompesch
Top achievements
Rank 1
Share this question
or