Skip to content

Commit 2dd92ba

Browse files
Tsvetomir-HrTsvetomir-Hr
authored andcommitted
kb(TreeList): article and example polish
1 parent e355e03 commit 2dd92ba

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

knowledge-base/treelist-expand-nodes-programmatically.md

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ ticketid: 1663716, 1649445, 1548181
2222

2323
## Description
2424

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.
25+
In the Telerik TreeList for Blazor, I load the TreeList in a collapsed mode. I need to programmatically expand and collapse TreeList nodes.
2626

2727
This KB article also answers the following questions:
2828
- How can I control the expanded state of TreeList nodes in code?
29-
- Is it possible to expand a TreeList node without user interaction?
29+
- Is it possible to expand a TreeList nodes without user interaction?
3030
- What approach should I take to programmatically adjust the visibility of TreeList nodes?
3131

3232
## Solution
@@ -36,45 +36,34 @@ To control the expanded or collapsed state of items in the TreeList programmatic
3636
Below is an example demonstrating how to use the TreeList State to expand or collapse items programmatically:
3737

3838
````CSHTML
39-
@using Telerik.DataSource;
40-
4139
<div>
4240
<span>
4341
<TelerikButton OnClick="@SetTreeListExpandedItems">Expand/Collapse All Items</TelerikButton>
4442
</span>
4543
</div>
4644
<br />
47-
<TelerikTreeList Data="@Data"
45+
46+
<TelerikTreeList @ref="TreeListRef"
47+
Data="@Data"
4848
ItemsField="@(nameof(Employee.DirectReports))"
49+
OnStateInit="((TreeListStateEventArgs<Employee> args) => OnStateInitHandler(args))"
4950
Reorderable="true"
5051
Resizable="true"
5152
Sortable="true"
5253
FilterMode="@TreeListFilterMode.FilterRow"
5354
Pageable="true"
54-
Width="850px"
55-
OnStateInit="((TreeListStateEventArgs<Employee> args) => OnStateInitHandler(args))"
56-
@ref="TreeListRef">
55+
Width="850px">
5756
<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" />
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" />
6260
</TreeListColumns>
6361
</TelerikTreeList>
6462
6563
@code {
6664
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-
}
65+
private List<Employee> Data { get; set; }
66+
private int LastId { get; set; } = 1;
7867
7968
private async Task SetTreeListExpandedItems()
8069
{
@@ -92,10 +81,12 @@ Below is an example demonstrating how to use the TreeList State to expand or col
9281
}
9382
}
9483
}
84+
9585
var expandedState = new TreeListState<Employee>()
9686
{
9787
ExpandedItems = new List<Employee>(toExpand)
9888
};
89+
9990
await TreeListRef.SetStateAsync(expandedState);
10091
}
10192
else
@@ -104,26 +95,23 @@ Below is an example demonstrating how to use the TreeList State to expand or col
10495
{
10596
ExpandedItems = new List<Employee>()
10697
};
98+
10799
await TreeListRef.SetStateAsync(expandedState);
108100
}
109101
110102
}
111103
112-
private List<Employee> Data { get; set; }
113-
114-
public class Employee
104+
private async Task OnStateInitHandler(TreeListStateEventArgs<Employee> args)
115105
{
116-
public List<Employee> DirectReports { get; set; }
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+
};
117111
118-
public int Id { get; set; }
119-
public string Name { get; set; }
120-
public string EmailAddress { get; set; }
121-
public DateTime HireDate { get; set; }
112+
args.TreeListState = collapsedItemsState;
122113
}
123114
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-
127115
protected override async Task OnInitializedAsync()
128116
{
129117
Data = await GetTreeListData();
@@ -178,6 +166,16 @@ Below is an example demonstrating how to use the TreeList State to expand or col
178166
179167
return await Task.FromResult(data);
180168
}
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+
}
181179
}
182180
````
183181

0 commit comments

Comments
 (0)