We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Programmatically select node by it's id

Hello,

i have the following data structure in my treeview:

{"id":'root',"parent_id":0,"name":"Root","subChild":[
{"id":1,"parent_id":0,"name":"Test Folder","subChild":[
{"id":2,"parent_id":1,"name":"Child 1"},
{"id":3,"parent_id":1,"name":"Child 2","subChild":[
{"id":4,"parent_id":3,"name":"Child 3"}
]
}
]
},
{"id":5,"parent_id":0,"name":"User 2 Folder 1"},
{"id":6,"parent_id":0,"name":"User 2 Folder 2"}]
}


Is it possible to select a node programmatically through it's id? For example i want to select the node with id 4.
Single node selection mode.

 
I tried

document.getElementById('treeview').ej2_instances[0].selectNode(id);

but without success.


Thanks in adavance


1 Reply

PO Prince Oliver Syncfusion Team April 8, 2019 10:44 AM UTC

Hello Christopher, 

Thanks for contacting us. 

You can programmatically select a node in TreeView by selectedNodes property. Here we provided the selectedNodes property in the dataBound event of TreeView. Kindly refer to the following code snippet. 

dataBound(args){ 
    if (args.data.length > 0) 
        this.$refs.treeObj.selectedNodes = ["4"] 
}, 

We have attached an example for reference, please find it in the following location 

For further reference, please refer the following API reference: 

Let us know if you need any further assistance. 

Regards, 
Prince 


Loader.
Up arrow icon