TelerikTreeView prevent selection from changing

1 Answer 11 Views
TreeView
Jim
Top achievements
Rank 1
Iron
Iron
Jim asked on 19 Jun 2024, 09:49 AM

I am using a TelerikTreeView in my component. When I click on a node (an expanded child node), I check whether the form for the currently selected node is dirty. If the form is dirty, I show a prompt asking the user to continue or not. When the user selects no/cancel, the clicked node should not be selected.

I am using the event below.

protected async Task OnItemClickedAsync(TreeViewItemClickEventArgs args)

I can't find a way to cancel this event or to prevent the newly clicked item to be selected. Is there any way to prevent a clicked item to be selected? I have been experimenting with @bind-SelectedItems to no avail. I haven't been able to find a solution for this problem.

Any help is appreciated.

1 Answer, 1 is accepted

Sort by
0
Radko
Telerik team
answered on 21 Jun 2024, 05:44 AM

Hi Jim,

Since the OnItemClick is really an informative event, it is not designed to be cancellable. What you are after should be possible by handling the SelectedItemsChanged event, as you have already attempted, thus I am curious as to what went wrong there.

The SelectedItemsChanged event fires when an end user performs selection by clicking on an item. When handling the event, you receive an argument list containing a list of items the user is attempting to select - you can then execute any logic you see fit to decide whether the selection should indeed go through or not. An important note is that if you handle this event, you will then be responsible to correctly update the SelectedItems parameter, as you are essentially intercepting the two-way binding.

Here is a simple example where selection is possible only for nodes with text containing index: https://blazorrepl.telerik.com/QoOAQbaz43hQ5x9714

Regards,
Radko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!
Jim
Top achievements
Rank 1
Iron
Iron
commented on 02 Jul 2024, 12:15 PM

Thanks for the reply. I think I am mixing two modes and I am not implementing ithemcorrectly.

I will check out your example and I will take a lookt at this page in more detail:

https://docs.telerik.com/blazor-ui/components/treeview/events#selecteditemschanged

Jim
Top achievements
Rank 1
Iron
Iron
commented on 02 Jul 2024, 12:29 PM

I got it to work!!! Thanks a lot.
Tags
TreeView
Asked by
Jim
Top achievements
Rank 1
Iron
Iron
Answers by
Radko
Telerik team
Share this question
or