Skip to content

Commit 8e3ff0f

Browse files
kendo-botKB BotTsvetomir-Hr
authored
Added new kb article treeview-disable-checkboxes (#2838)
* Added new kb article treeview-disable-checkboxes * chore: polish example --------- Co-authored-by: KB Bot <kb-bot@telerik.com> Co-authored-by: Tsvetomir Hristov <106250052+Tsvetomir-Hr@users.noreply.github.com>
1 parent c474b15 commit 8e3ff0f

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
title: How to Disable Checkboxes for Certain TreeView Items
3+
description: Learn how to disable checkboxes for certain items in the TreeView component based on a condition
4+
type: how-to
5+
page_title: How to Disable Checkboxes for Certain TreeView Items
6+
slug: treeview-kb-disable-checkboxes
7+
tags: blazor, treeview, checkbox, disabled
8+
res_type: kb
9+
ticketid: 1681641
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>TreeView for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
This knowledge base article answers the following questions:
26+
27+
- How can I apply custom CSS to TreeView items conditionally?
28+
- How to use the [`OnItemRender` event](slug:treeview-events#onitemrender) in the TreeView for Blazor?
29+
- Is it possible to disable checkboxes for certain TreeView items?
30+
31+
## Solution
32+
33+
To disable checkboxes for specific TreeView items based on a condition, use the `OnItemRender` event to apply a custom class to those items. Then, use CSS to style these items and their checkboxes as disabled. The following steps outline how to achieve this:
34+
35+
1. Use the [`OnItemRender` event](slug:treeview-events#onitemrender) of the TreeView component to conditionally apply a custom CSS class.
36+
37+
2. Define CSS styles that mimic the disabled state for the checkboxes and the items.
38+
39+
3. Ensure your TreeView model includes a property (e.g., `IsActive`, `IsDisabled`)that you'll use to determine if an item's checkbox should be disabled.
40+
41+
### Implementation
42+
43+
````RAZOR
44+
<TelerikTreeView Data="@FlatData"
45+
@bind-ExpandedItems="@ExpandedItems"
46+
CheckBoxMode="@TreeViewCheckBoxMode.Multiple"
47+
@bind-CheckedItems="@CheckedItems"
48+
OnItemRender="@HandleOnItemRender">
49+
<TreeViewBindings>
50+
<TreeViewBinding IdField="Id" ParentIdField="ParentIdValue" TextField="Text" HasChildrenField="HasChildren" IconField="Icon" />
51+
</TreeViewBindings>
52+
</TelerikTreeView>
53+
54+
<style>
55+
.@(DisabledCheckboxClass) .k-checkbox {
56+
opacity: 0.5;
57+
pointer-events: none;
58+
}
59+
60+
.@(DisabledCheckboxClass) .k-treeview-leaf {
61+
opacity: 0.7;
62+
pointer-events: none;
63+
}
64+
</style>
65+
66+
@code {
67+
private IEnumerable<TreeViewItem> FlatData { get; set; }
68+
private IEnumerable<object> CheckedItems { get; set; } = new List<object>();
69+
private IEnumerable<object> ExpandedItems { get; set; } = new List<TreeViewItem>();
70+
private string DisabledCheckboxClass { get; set; } = "disabled-checkbox";
71+
72+
private void HandleOnItemRender(TreeViewItemRenderEventArgs args)
73+
{
74+
var item = args.Item as TreeViewItem;
75+
if (!item.IsActive)
76+
{
77+
args.Class = DisabledCheckboxClass;
78+
}
79+
}
80+
protected override void OnInitialized()
81+
{
82+
LoadFlatData();
83+
ExpandedItems = FlatData.Where(x => x.HasChildren).ToList();
84+
}
85+
86+
private void LoadFlatData()
87+
{
88+
List<TreeViewItem> items = new List<TreeViewItem>();
89+
90+
items.Add(new TreeViewItem()
91+
{
92+
Id = 1,
93+
Text = "Root",
94+
ParentIdValue = null,
95+
HasChildren = true,
96+
Icon = SvgIcon.Folder,
97+
IsChecked = false,
98+
IsActive = true
99+
});
100+
101+
Random rnd = new Random();
102+
for (int i = 2; i <= 5; i++)
103+
{
104+
bool hasChildren = rnd.Next(0, 2) == 1;
105+
items.Add(new TreeViewItem()
106+
{
107+
Id = i,
108+
Text = $"Folder {i}",
109+
ParentIdValue = 1,
110+
HasChildren = hasChildren,
111+
Icon = hasChildren ? SvgIcon.Folder : SvgIcon.File,
112+
IsChecked = false,
113+
IsActive = rnd.Next(0, 2) == 1
114+
});
115+
116+
if (hasChildren)
117+
{
118+
for (int j = 1; j <= rnd.Next(1, 4); j++)
119+
{
120+
int childId = i * 10 + j;
121+
items.Add(new TreeViewItem()
122+
{
123+
Id = childId,
124+
Text = $"File {childId}",
125+
ParentIdValue = i,
126+
HasChildren = false,
127+
Icon = SvgIcon.File,
128+
IsChecked = false,
129+
IsActive = rnd.Next(0, 2) == 1
130+
});
131+
}
132+
}
133+
}
134+
135+
FlatData = items;
136+
}
137+
138+
public class TreeViewItem
139+
{
140+
public int Id { get; set; }
141+
public string Text { get; set; } = string.Empty;
142+
public int? ParentIdValue { get; set; }
143+
public bool HasChildren { get; set; }
144+
public ISvgIcon? Icon { get; set; }
145+
public bool IsChecked { get; set; }
146+
public bool IsActive { get; set; }
147+
}
148+
}
149+
````
150+
151+
## See Also
152+
153+
- [TreeView Overview](slug:treeview-overview)
154+
- [TreeView OnItemRender Event](slug:treeview-events#onitemrender)

0 commit comments

Comments
 (0)