Switch instead of checkbox in TreeView

1 Answer 21 Views
Switch TreeView
Justin
Top achievements
Rank 1
Justin asked on 31 Dec 2024, 10:19 PM

Is there a way to use a Switch control instead of checkbox in a TreeView? I am able to use ItemTemplate to insert a switch next to the checkbox but I need to replace the checkbox and have the switch use CheckChildren and CheckParents.

Any ideas?

Thanks!

<TelerikTreeView OnItemRender="@OnItemRender" CheckBoxMode="TreeViewCheckBoxMode.Multiple" CheckChildren="true" CheckParents="true" Data="@FlatData" @bind-ExpandedItems="@ExpandedItems">
    <TreeViewBindings>
        <TreeViewBinding TextField="Text" IdField="Id" ParentIdField="ParentId" ItemsField="Text" HasChildrenField="HasChildren" IconField="Icon">
            <ItemTemplate>
                @{var treeItem = (TreeItem)context; }
                <div class="treeview-item">
                    <TelerikSwitch @bind-Value="treeItem.IsChecked" />
                    <span>@treeItem.Text</span>
                </div>
            </ItemTemplate>
        </TreeViewBinding>
    </TreeViewBindings>
</TelerikTreeView>

1 Answer, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 02 Jan 2025, 08:01 AM

Hello Justin,

When using checkboxes or a similar component inside the TreeView ItemTemplate, the CheckParents and CheckChildren logic must be implemented manually. You will need a ValueChanged or OnChange event for the Switch.

A possible workaround that will make your job easier is to render the built-in checkboxes and hide them with CSS. On Switch toggle, click the corresponding checkbox with JavaScript, which will execute the built-in check parents/children logic.

One downside of using Switches instead of TreeView checkboxes is the lack of indeterminate state.

On a side note, please ask the license holder at your company to assign you a license, so that your account complies with our license agreement.

Regards,
Dimo
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Switch TreeView
Asked by
Justin
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Share this question
or