Setting selected item programmatically in Perspective tree

I’m trying to set the selected item programmatically in a Tree component but am having trouble finding any documentation on how to do this. It didn’t seem to work when I tried setting selection/selectionData. Specifically, I’m doing this within an onStartup event script so that on page load (or component load), one of the rows is selected automatically.

You should be able to set the selection prop with a list containing the path for your item. The path consists of index numbers and / for every level of selection. So for example 0/0/0 = Item 1/Child 1/Grandchild1 and 0/0/1 = Item 1/Child 1/Grandchild2

This code worked for me on a button. If you want to use an onStartup event make sure your component path is correct.

selectionList = ['0/0/0', '0/0/1']
self.getSibling("Tree").props.selection = selectionList

Edit: For clarification, the selectionData handles itself automatically.

2 Likes

Well huh, I thought that’s what I was doing before but I must have been doing something wrong. That worked, thank you!

1 Like

The only problem you could experience is that the selection won’t show up if the path is collapsed. You’ll just have to make sure the expanded prop inside of items is set to true for the correct paths. Simple enough.

self.getSibling("Tree").props.items[index]["expanded"] = 1
#for any children there is another items list
self.getSibling("Tree").props.items[index]["Items"][index]["expanded"] = 1
1 Like