Is there a way to make the treeview scroll so that a speficic node is visible? I need to make the node visible that was selected by code. So I am looking for a way to do what the .BringIntoView() method in the WPF treeview does...
thanks and regards
Sven
1 Answer, 1 is accepted
0
Accepted
Dimo
Telerik team
answered on 19 Jan 2022, 03:07 PM
| edited on 27 Jan 2022, 05:31 PM
Execute the custom JavaScript. You can find the TreeView item by its "k-state-selected" CSS class. You may need to use setTimeout to wait for actions 1 and 2 to complete.
Update: Here is how to find the selected TreeView item with JavaScript. Note that in UI for Blazor 3.0 the k-state-selected class was renamed to k-selected. If you have multiple TreeViews on the page, use a custom class instead of k-treeview.
var item = document.querySelector(".k-treeview .k-selected");
if (item) {
item.scrollIntoView();
}
Thanks. But I am a bit struggling with point 3. So far I have expanded and selected the item in my C# code. I think I would need to trigger a javascript function that searches for the selected element in the treeview. Can You give me hint of how to do that? I think it would be a goog idea to add a hidden element with an identifier to my item template. So I need to provide the id to the javascript function as a param.
But at the time I call the javascript method 'ScrollToNode' the tree is
not yet exanded in Browser. So, the element does not yet exist in DOM.
Do you have any Ideo how I can call the javascript method after(!) client side changes are rendered?
Thanks in advance Sven
Dimo
Telerik team
commented on 01 Feb 2022, 11:31 AM
@Sven - a possible approach is to raise a boolean flag where you expand the TreeView, and then use this flag in OnAfterRenderAsync to call the JavaScript code.