[GH-ISSUE #896] Add func (*TreeNode) GetParent #653

Open
opened 2026-03-04 01:06:47 +03:00 by kerem · 6 comments
Owner

Originally created by @paololazzari on GitHub (Oct 4, 2023).
Original GitHub issue: https://github.com/rivo/tview/issues/896

Being able to get the parent of a given node would make traversing the tree much easier

Originally created by @paololazzari on GitHub (Oct 4, 2023). Original GitHub issue: https://github.com/rivo/tview/issues/896 Being able to get the parent of a given node would make traversing the tree much easier
Author
Owner

@rivo commented on GitHub (Oct 5, 2023):

Can you give some details how this would help you in your specific application? There is Walk() which is a public function, which also gives you parent information for each traversed node.

<!-- gh-comment-id:1748348981 --> @rivo commented on GitHub (Oct 5, 2023): Can you give some details how this would help you in your specific application? There is [`Walk()`](https://pkg.go.dev/github.com/rivo/tview#TreeNode.Walk) which is a public function, which also gives you parent information for each traversed node.
Author
Owner

@paololazzari commented on GitHub (Oct 5, 2023):

@rivo I have something similar to the demo here https://github.com/rivo/tview/tree/master/demos/treeview . When I press enter and a node which corresponds to a valid file is selected, I want to open that file in a textview. Opening the file for files within folders requires having the name of the parent nodes too

<!-- gh-comment-id:1748688432 --> @paololazzari commented on GitHub (Oct 5, 2023): @rivo I have something similar to the demo here https://github.com/rivo/tview/tree/master/demos/treeview . When I press enter and a node which corresponds to a valid file is selected, I want to open that file in a textview. Opening the file for files within folders requires having the name of the parent nodes too
Author
Owner

@rivo commented on GitHub (Oct 5, 2023):

Isn't your actual requirement then to get the entire path to the current node, not just to the parent? Like the breadcrumbs you see on many websites?

If so, a better solution would then be to provide a GetPath(node *TreeNode) []*TreeNode method which gives you all nodes leading to your current node, starting at the root.

What do you think?

<!-- gh-comment-id:1748758014 --> @rivo commented on GitHub (Oct 5, 2023): Isn't your actual requirement then to get the entire path to the current node, not just to the parent? Like the breadcrumbs you see on many websites? If so, a better solution would then be to provide a `GetPath(node *TreeNode) []*TreeNode` method which gives you all nodes leading to your current node, starting at the root. What do you think?
Author
Owner

@paololazzari commented on GitHub (Oct 5, 2023):

Yes, absolutely!

<!-- gh-comment-id:1748772479 --> @paololazzari commented on GitHub (Oct 5, 2023): Yes, absolutely!
Author
Owner

@paololazzari commented on GitHub (Oct 5, 2023):

Actually, this seems to be simple enough already. As long as the reference is set to the full path (as shown here then its trivial to get the full path of the selected node with GetCurrentNode().GetReference().(string)

<!-- gh-comment-id:1748818214 --> @paololazzari commented on GitHub (Oct 5, 2023): Actually, this seems to be simple enough already. As long as the reference is set to the full path (as shown [here](https://github.com/rivo/tview/blob/master/demos/treeview/main.go#L30) then its trivial to get the full path of the selected node with `GetCurrentNode().GetReference().(string)`
Author
Owner

@rivo commented on GitHub (Oct 5, 2023):

Well, I added the function (check out the latest commit) while I was travelling and offline so I only saw your message afterwards. But you are right, in the demo, the full path is stored as a reference object so it's available anyway.

<!-- gh-comment-id:1749657207 --> @rivo commented on GitHub (Oct 5, 2023): Well, I added the function (check out the latest commit) while I was travelling and offline so I only saw your message afterwards. But you are right, in the demo, the full path is stored as a reference object so it's available anyway.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#653
No description provided.