Skip to content

Commit 2a3612f

Browse files
author
KB Bot
committed
Added new kb article treeview-disable-checkboxes
1 parent 20b9197 commit 2a3612f

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
.disabled-checkbox .k-checkbox {
56+
opacity: 0.7;
57+
pointer-events: none;
58+
}
59+
60+
.disabled-checkbox .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+
71+
private void HandleOnItemRender(TreeViewItemRenderEventArgs args)
72+
{
73+
var item = args.Item as TreeViewItem;
74+
if (!item.IsActive)
75+
{
76+
args.Class = "disabled-checkbox";
77+
}
78+
}
79+
protected override void OnInitialized()
80+
{
81+
LoadFlatData();
82+
ExpandedItems = FlatData.Where(x => x.HasChildren).ToList();
83+
}
84+
85+
private void LoadFlatData()
86+
{
87+
List<TreeViewItem> items = new List<TreeViewItem>();
88+
89+
items.Add(new TreeViewItem()
90+
{
91+
Id = 1,
92+
Text = "Root",
93+
ParentIdValue = null,
94+
HasChildren = true,
95+
Icon = SvgIcon.Folder,
96+
IsChecked = false,
97+
IsActive = true
98+
});
99+
100+
Random rnd = new Random();
101+
for (int i = 2; i <= 5; i++)
102+
{
103+
bool hasChildren = rnd.Next(0, 2) == 1;
104+
items.Add(new TreeViewItem()
105+
{
106+
Id = i,
107+
Text = $"Folder {i}",
108+
ParentIdValue = 1,
109+
HasChildren = hasChildren,
110+
Icon = hasChildren ? SvgIcon.Folder : SvgIcon.File,
111+
IsChecked = false,
112+
IsActive = rnd.Next(0, 2) == 1
113+
});
114+
115+
if (hasChildren)
116+
{
117+
for (int j = 1; j <= rnd.Next(1, 4); j++)
118+
{
119+
int childId = i * 10 + j;
120+
items.Add(new TreeViewItem()
121+
{
122+
Id = childId,
123+
Text = $"File {childId}",
124+
ParentIdValue = i,
125+
HasChildren = false,
126+
Icon = SvgIcon.File,
127+
IsChecked = false,
128+
IsActive = rnd.Next(0, 2) == 1
129+
});
130+
}
131+
}
132+
}
133+
134+
FlatData = items;
135+
}
136+
137+
public class TreeViewItem
138+
{
139+
public int Id { get; set; }
140+
public string Text { get; set; }
141+
public int? ParentIdValue { get; set; }
142+
public bool HasChildren { get; set; }
143+
public ISvgIcon Icon { get; set; }
144+
public bool IsChecked { get; set; }
145+
public bool IsActive { get; set; }
146+
}
147+
}
148+
````
149+
150+
## See Also
151+
152+
- [TreeView Overview](slug:treeview-overview)
153+
- [TreeView OnItemRender Event](slug:treeview-events#onitemrender)

0 commit comments

Comments
 (0)