|
| 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 TreeList nodes. |
| 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 nodes 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 | +<div> |
| 40 | + <span> |
| 41 | + <TelerikButton OnClick="@SetTreeListExpandedItems">Expand/Collapse All Items</TelerikButton> |
| 42 | + </span> |
| 43 | +</div> |
| 44 | +<br /> |
| 45 | +
|
| 46 | +<TelerikTreeList @ref="TreeListRef" |
| 47 | + Data="@Data" |
| 48 | + ItemsField="@(nameof(Employee.DirectReports))" |
| 49 | + OnStateInit="((TreeListStateEventArgs<Employee> args) => OnStateInitHandler(args))" |
| 50 | + Reorderable="true" |
| 51 | + Resizable="true" |
| 52 | + Sortable="true" |
| 53 | + FilterMode="@TreeListFilterMode.FilterRow" |
| 54 | + Pageable="true" |
| 55 | + Width="850px"> |
| 56 | + <TreeListColumns> |
| 57 | + <TreeListColumn Field="@nameof(Employee.Name)" Expandable="true" Width="320px" /> |
| 58 | + <TreeListColumn Field="@nameof(Employee.EmailAddress)" Width="220px" /> |
| 59 | + <TreeListColumn Field="@nameof(Employee.HireDate)" Width="220px" /> |
| 60 | + </TreeListColumns> |
| 61 | +</TelerikTreeList> |
| 62 | +
|
| 63 | +@code { |
| 64 | + private TelerikTreeList<Employee> TreeListRef { get; set; } = new TelerikTreeList<Employee>(); |
| 65 | + private List<Employee> Data { get; set; } |
| 66 | + private int LastId { get; set; } = 1; |
| 67 | +
|
| 68 | + private async Task SetTreeListExpandedItems() |
| 69 | + { |
| 70 | + if (!(TreeListRef.GetState().ExpandedItems.Any())) |
| 71 | + { |
| 72 | + List<Employee> toExpand = new List<Employee>(); |
| 73 | + foreach (Employee item in Data) |
| 74 | + { |
| 75 | + toExpand.Add(item); |
| 76 | + if (item.DirectReports.Any()) |
| 77 | + { |
| 78 | + foreach (Employee child in item.DirectReports) |
| 79 | + { |
| 80 | + toExpand.Add(child); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +
|
| 85 | + var expandedState = new TreeListState<Employee>() |
| 86 | + { |
| 87 | + ExpandedItems = new List<Employee>(toExpand) |
| 88 | + }; |
| 89 | +
|
| 90 | + await TreeListRef.SetStateAsync(expandedState); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + var expandedState = new TreeListState<Employee>() |
| 95 | + { |
| 96 | + ExpandedItems = new List<Employee>() |
| 97 | + }; |
| 98 | +
|
| 99 | + await TreeListRef.SetStateAsync(expandedState); |
| 100 | + } |
| 101 | +
|
| 102 | + } |
| 103 | +
|
| 104 | + private async Task OnStateInitHandler(TreeListStateEventArgs<Employee> args) |
| 105 | + { |
| 106 | + var collapsedItemsState = new TreeListState<Employee>() |
| 107 | + { |
| 108 | + //collapse all items in the TreeList upon initialization of the state |
| 109 | + ExpandedItems = new List<Employee>() |
| 110 | + }; |
| 111 | +
|
| 112 | + args.TreeListState = collapsedItemsState; |
| 113 | + } |
| 114 | +
|
| 115 | + protected override async Task OnInitializedAsync() |
| 116 | + { |
| 117 | + Data = await GetTreeListData(); |
| 118 | + } |
| 119 | +
|
| 120 | + private async Task<List<Employee>> GetTreeListData() |
| 121 | + { |
| 122 | + List<Employee> data = new List<Employee>(); |
| 123 | +
|
| 124 | + for (int i = 1; i < 15; i++) |
| 125 | + { |
| 126 | + Employee root = new Employee |
| 127 | + { |
| 128 | + Id = LastId, |
| 129 | + Name = $"root: {i}", |
| 130 | + EmailAddress = $"{i}@example.com", |
| 131 | + HireDate = DateTime.Now.AddYears(-i), |
| 132 | + DirectReports = new List<Employee>(), // prepare a collection for the child items, will be populated later in the code |
| 133 | + }; |
| 134 | + data.Add(root); |
| 135 | + LastId++; |
| 136 | +
|
| 137 | + for (int j = 1; j < 4; j++) |
| 138 | + { |
| 139 | + int currId = LastId; |
| 140 | + Employee firstLevelChild = new Employee |
| 141 | + { |
| 142 | + Id = currId, |
| 143 | + Name = $"first level child {j} of {i}", |
| 144 | + EmailAddress = $"{currId}@example.com", |
| 145 | + HireDate = DateTime.Now.AddDays(-currId), |
| 146 | + DirectReports = new List<Employee>(), // collection for child nodes |
| 147 | + }; |
| 148 | + root.DirectReports.Add(firstLevelChild); // populate the parent's collection |
| 149 | + LastId++; |
| 150 | +
|
| 151 | + for (int k = 1; k < 3; k++) |
| 152 | + { |
| 153 | + int nestedId = LastId; |
| 154 | + // populate the parent's collection |
| 155 | + firstLevelChild.DirectReports.Add(new Employee |
| 156 | + { |
| 157 | + Id = LastId, |
| 158 | + Name = $"second level child {k} of {j} and {i}", |
| 159 | + EmailAddress = $"{nestedId}@example.com", |
| 160 | + HireDate = DateTime.Now.AddMinutes(-nestedId) |
| 161 | + }); ; |
| 162 | + LastId++; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +
|
| 167 | + return await Task.FromResult(data); |
| 168 | + } |
| 169 | +
|
| 170 | + public class Employee |
| 171 | + { |
| 172 | + public List<Employee> DirectReports { get; set; } |
| 173 | +
|
| 174 | + public int Id { get; set; } |
| 175 | + public string Name { get; set; } |
| 176 | + public string EmailAddress { get; set; } |
| 177 | + public DateTime HireDate { get; set; } |
| 178 | + } |
| 179 | +} |
| 180 | +```` |
| 181 | + |
| 182 | +## See Also |
| 183 | + |
| 184 | +- [TreeList State Documentation]({%slug treelist-state%}) |
| 185 | +- [TreeList Overview]({%slug treelist-overview%}) |
0 commit comments