Skip to content

Commit e355e03

Browse files
KB BotTsvetomir-Hr
authored andcommitted
Added new kb article treelist-expand-nodes-programmatically
1 parent 6ec129a commit e355e03

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
---
2+
title: How to Programmatically Expand or Collapse TreeList Nodes
3+
description: Learn how to programmatically expand or collapse nodes in a Telerik TreeList for Blazor by utilizing the TreeList state management.
4+
type: how-to
5+
page_title: How to Programmatically Expand or Collapse TreeList Nodes
6+
slug: treelist-kb-expand-nodes-programmatically
7+
tags: treelist, expand, collapse
8+
res_type: kb
9+
ticketid: 1663716, 1649445, 1548181
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>TreeList for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
In the Telerik TreeList for Blazor, I load the TreeList in a collapsed mode. I need to programmatically expand and collapse a specific node programmatically.
26+
27+
This KB article also answers the following questions:
28+
- How can I control the expanded state of TreeList nodes in code?
29+
- Is it possible to expand a TreeList node without user interaction?
30+
- What approach should I take to programmatically adjust the visibility of TreeList nodes?
31+
32+
## Solution
33+
34+
To control the expanded or collapsed state of items in the TreeList programmatically, utilize the TreeList State. The state management feature allows for the adjustment of item visibility through code.
35+
36+
Below is an example demonstrating how to use the TreeList State to expand or collapse items programmatically:
37+
38+
````CSHTML
39+
@using Telerik.DataSource;
40+
41+
<div>
42+
<span>
43+
<TelerikButton OnClick="@SetTreeListExpandedItems">Expand/Collapse All Items</TelerikButton>
44+
</span>
45+
</div>
46+
<br />
47+
<TelerikTreeList Data="@Data"
48+
ItemsField="@(nameof(Employee.DirectReports))"
49+
Reorderable="true"
50+
Resizable="true"
51+
Sortable="true"
52+
FilterMode="@TreeListFilterMode.FilterRow"
53+
Pageable="true"
54+
Width="850px"
55+
OnStateInit="((TreeListStateEventArgs<Employee> args) => OnStateInitHandler(args))"
56+
@ref="TreeListRef">
57+
<TreeListColumns>
58+
<TreeListColumn Field="Name" Expandable="true" Width="320px" />
59+
<TreeListColumn Field="Id" Editable="false" Width="120px" />
60+
<TreeListColumn Field="EmailAddress" Width="220px" />
61+
<TreeListColumn Field="HireDate" Width="220px" />
62+
</TreeListColumns>
63+
</TelerikTreeList>
64+
65+
@code {
66+
private TelerikTreeList<Employee> TreeListRef { get; set; } = new TelerikTreeList<Employee>();
67+
68+
private async Task OnStateInitHandler(TreeListStateEventArgs<Employee> args)
69+
{
70+
var collapsedItemsState = new TreeListState<Employee>()
71+
{
72+
//collapse all items in the TreeList upon initialization of the state
73+
ExpandedItems = new List<Employee>()
74+
};
75+
76+
args.TreeListState = collapsedItemsState;
77+
}
78+
79+
private async Task SetTreeListExpandedItems()
80+
{
81+
if (!(TreeListRef.GetState().ExpandedItems.Any()))
82+
{
83+
List<Employee> toExpand = new List<Employee>();
84+
foreach (Employee item in Data)
85+
{
86+
toExpand.Add(item);
87+
if (item.DirectReports.Any())
88+
{
89+
foreach (Employee child in item.DirectReports)
90+
{
91+
toExpand.Add(child);
92+
}
93+
}
94+
}
95+
var expandedState = new TreeListState<Employee>()
96+
{
97+
ExpandedItems = new List<Employee>(toExpand)
98+
};
99+
await TreeListRef.SetStateAsync(expandedState);
100+
}
101+
else
102+
{
103+
var expandedState = new TreeListState<Employee>()
104+
{
105+
ExpandedItems = new List<Employee>()
106+
};
107+
await TreeListRef.SetStateAsync(expandedState);
108+
}
109+
110+
}
111+
112+
private List<Employee> Data { get; set; }
113+
114+
public class Employee
115+
{
116+
public List<Employee> DirectReports { get; set; }
117+
118+
public int Id { get; set; }
119+
public string Name { get; set; }
120+
public string EmailAddress { get; set; }
121+
public DateTime HireDate { get; set; }
122+
}
123+
124+
// used in this example for data generation and retrieval for CUD operations on the current view-model data
125+
public int LastId { get; set; } = 1;
126+
127+
protected override async Task OnInitializedAsync()
128+
{
129+
Data = await GetTreeListData();
130+
}
131+
132+
private async Task<List<Employee>> GetTreeListData()
133+
{
134+
List<Employee> data = new List<Employee>();
135+
136+
for (int i = 1; i < 15; i++)
137+
{
138+
Employee root = new Employee
139+
{
140+
Id = LastId,
141+
Name = $"root: {i}",
142+
EmailAddress = $"{i}@example.com",
143+
HireDate = DateTime.Now.AddYears(-i),
144+
DirectReports = new List<Employee>(), // prepare a collection for the child items, will be populated later in the code
145+
};
146+
data.Add(root);
147+
LastId++;
148+
149+
for (int j = 1; j < 4; j++)
150+
{
151+
int currId = LastId;
152+
Employee firstLevelChild = new Employee
153+
{
154+
Id = currId,
155+
Name = $"first level child {j} of {i}",
156+
EmailAddress = $"{currId}@example.com",
157+
HireDate = DateTime.Now.AddDays(-currId),
158+
DirectReports = new List<Employee>(), // collection for child nodes
159+
};
160+
root.DirectReports.Add(firstLevelChild); // populate the parent's collection
161+
LastId++;
162+
163+
for (int k = 1; k < 3; k++)
164+
{
165+
int nestedId = LastId;
166+
// populate the parent's collection
167+
firstLevelChild.DirectReports.Add(new Employee
168+
{
169+
Id = LastId,
170+
Name = $"second level child {k} of {j} and {i}",
171+
EmailAddress = $"{nestedId}@example.com",
172+
HireDate = DateTime.Now.AddMinutes(-nestedId)
173+
}); ;
174+
LastId++;
175+
}
176+
}
177+
}
178+
179+
return await Task.FromResult(data);
180+
}
181+
}
182+
````
183+
184+
## See Also
185+
186+
- [TreeList State Documentation]({%slug treelist-state%})
187+
- [TreeList Overview]({%slug treelist-overview%})

0 commit comments

Comments
 (0)