|
| 1 | +--- |
| 2 | +title: How to Initially Display the Page with a Pre-Selected Node in TreeList |
| 3 | +description: Learn how to ensure a pre-selected node is visible by automatically navigating to its page in a Telerik TreeList with pagination enabled. |
| 4 | +type: how-to |
| 5 | +page_title: Navigate to Page Containing Pre-Selected Node in TreeList |
| 6 | +meta_title: Navigate to Page Containing Pre-Selected Node in TreeList |
| 7 | +slug: treelist-kb-paging-pre-selected-node |
| 8 | +tags: treelist, paging, pre-selected, node, navigation, page, size, blazor |
| 9 | +res_type: kb |
| 10 | +ticketid: 1690423 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | + <tbody> |
| 17 | + <tr> |
| 18 | + <td>Product</td> |
| 19 | + <td>TreeList for Blazor</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +I have a Telerik [TreeList](slug:treelist-overview) with a checkbox for selection and pagination enabled. I pre-select a node programmatically and need the TreeList to automatically navigate to the page containing the first selected node. |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +To automatically show the page that contains the first pre-selected node in your Telerik TreeList with paging enabled, you need to programmatically set the TreeList's `Page` property based on the index of the selected node in your data. |
| 31 | + |
| 32 | +1. Identify the index of the first selected node in your data source. |
| 33 | +2. Use the PageSize to determine on which page the node appears. |
| 34 | +3. Assign the calculated page number to the TreeList's Page parameter before rendering. |
| 35 | + |
| 36 | +````Razor |
| 37 | +<TelerikTreeList Data="@TreeListData" |
| 38 | + IdField="@nameof(Employee.Id)" |
| 39 | + ParentIdField="@nameof(Employee.ParentId)" |
| 40 | + SelectionMode="@TreeListSelectionMode.Multiple" |
| 41 | + @bind-SelectedItems="@SelectedEmployees" |
| 42 | + Pageable="true" |
| 43 | + @bind-PageSize="@PageSize" |
| 44 | + @bind-Page="@CurrentPage"> |
| 45 | + <TreeListColumns> |
| 46 | + <TreeListCheckboxColumn Width="50px" /> |
| 47 | + <TreeListColumn Field="@nameof(Employee.FirstName)" Title="First Name" Width="350px" Expandable="true" /> |
| 48 | + <TreeListColumn Field="@nameof(Employee.LastName)" Title="Last Name" /> |
| 49 | + <TreeListColumn Field="@nameof(Employee.Position)" Title="Position" Width="200px" /> |
| 50 | + </TreeListColumns> |
| 51 | +</TelerikTreeList> |
| 52 | +
|
| 53 | +@code { |
| 54 | + private List<Employee> TreeListData { get; set; } = new(); |
| 55 | + private IEnumerable<Employee> SelectedEmployees { get; set; } = Enumerable.Empty<Employee>(); |
| 56 | + private int PageSize = 10; |
| 57 | + private int CurrentPage = 1; |
| 58 | +
|
| 59 | + protected override void OnInitialized() |
| 60 | + { |
| 61 | + TreeListData = new List<Employee>(); |
| 62 | +
|
| 63 | + for (int i = 1; i <= 59; i++) |
| 64 | + { |
| 65 | + TreeListData.Add(new Employee() |
| 66 | + { |
| 67 | + Id = i, |
| 68 | + ParentId = i <= 3 ? null : i % 3 + 1, |
| 69 | + FirstName = "First " + i, |
| 70 | + LastName = "Last " + i, |
| 71 | + Position = i <= 3 ? "Team Lead" : "Software Engineer" |
| 72 | + }); |
| 73 | + } |
| 74 | +
|
| 75 | + SelectedEmployees = new List<Employee>() { TreeListData.ElementAt(2) }; |
| 76 | +
|
| 77 | + var selectedId = SelectedEmployees.FirstOrDefault()?.Id; |
| 78 | + if (selectedId != null) |
| 79 | + { |
| 80 | + // Step 1: Flatten the tree as it would appear expanded |
| 81 | + var flatList = new List<Employee>(); |
| 82 | + foreach (var root in TreeListData.Where(e => e.ParentId == null)) |
| 83 | + { |
| 84 | + FlattenHierarchy(root, flatList); |
| 85 | + } |
| 86 | +
|
| 87 | + // Step 2: Find index of selected item in flattened list |
| 88 | + var index = flatList.FindIndex(e => e.Id == selectedId); |
| 89 | + if (index >= 0) |
| 90 | + { |
| 91 | + CurrentPage = (index / PageSize) + 1; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +
|
| 96 | + private void FlattenHierarchy(Employee node, List<Employee> result) |
| 97 | + { |
| 98 | + result.Add(node); |
| 99 | + var children = TreeListData.Where(e => e.ParentId == node.Id); |
| 100 | + foreach (var child in children) |
| 101 | + { |
| 102 | + FlattenHierarchy(child, result); |
| 103 | + } |
| 104 | + } |
| 105 | +
|
| 106 | + public class Employee |
| 107 | + { |
| 108 | + public int Id { get; set; } |
| 109 | + public int? ParentId { get; set; } |
| 110 | + public string FirstName { get; set; } = string.Empty; |
| 111 | + public string LastName { get; set; } = string.Empty; |
| 112 | + public string Position { get; set; } = string.Empty; |
| 113 | + } |
| 114 | +} |
| 115 | +```` |
| 116 | + |
| 117 | +## See Also |
| 118 | + |
| 119 | +* [TreeList Overview](slug:treelist-overview) |
| 120 | +* [TreeList Paging](slug:treelist-paging) |
0 commit comments