|
| 1 | +--- |
| 2 | +title: TreeView Not Showing Horizontal Scrollbar |
| 3 | +description: How to enable horizontal and vertical scrolling for the Telerik Blazor TreeeView |
| 4 | +type: troubleshooting |
| 5 | +page_title: TreeView Doesn't Display Horizontal Scrollbar |
| 6 | +slug: treeview-kb-horizontal-scrollbar |
| 7 | +position: |
| 8 | +tags: telerik,blazor,treeview,scrolling |
| 9 | +ticketid: 1585834, 1558530, 1557541 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | + <tbody> |
| 17 | + <tr> |
| 18 | + <td>Product</td> |
| 19 | + <td>TreeView for Blazor</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +The TreeView control is not showing a horizontal scroll bar, even though the TreeView items overflow their container. Some items are not fully visible. |
| 27 | + |
| 28 | +The TreeView does not allow horizontal scrolling when some items extend past the viewport. Long items get cut off and can't be scrolled to. This also happens when the browser is zoomed in. |
| 29 | + |
| 30 | +How to force a scroll bar to appear in the Blazor TreeView component? |
| 31 | + |
| 32 | +## Cause\Possible Cause(s) |
| 33 | + |
| 34 | +The TreeView renders `div.k-animation-container` elements with an `overflow: hidden` CSS style. These elements wrap nested items and prohibit overflowing content during expand and collapse animations. |
| 35 | + |
| 36 | +## Solution |
| 37 | + |
| 38 | +1. Set a custom CSS class to the TreeView via the `Class` parameter. Alternatively, you can use the default `.k-treeview` class to target all TreeView instances on the page or in the app. |
| 39 | +1. Override the `overflow: hidden` style of the `div.k-animation-container` elements **inside** the TreeView. Set it to the default value of `overflow: visible`. |
| 40 | +1. Apply dimesions to trigger scrolling: |
| 41 | + * If you want the TreeView itself to show scrollbars, set `width` or `height` styles to it. |
| 42 | + * If you want the TreeView container to show scrollbars, set `width`, `height` and `overflow` styles to the container. |
| 43 | + |
| 44 | +The example below shows how to implement both options - scroll the TreeView and scroll its parent container. |
| 45 | + |
| 46 | +>caption Enable TreeView horizontal and vertical scrolling |
| 47 | +
|
| 48 | +````CSHTML |
| 49 | +<h2>TreeView Component Scrolling</h2> |
| 50 | +
|
| 51 | +<TelerikTreeView Data="@FlatData" |
| 52 | + @bind-ExpandedItems="@ExpandedItems" |
| 53 | + Class="scrollable-treeview" /> |
| 54 | +
|
| 55 | +<style> |
| 56 | + /* the TreeView has overflow:auto by default, so just set dimensions */ |
| 57 | + .scrollable-treeview { |
| 58 | + width: 300px; |
| 59 | + height: 300px; |
| 60 | + } |
| 61 | + /* allow the TreeView CONTENT to overflow and trigger scrolling */ |
| 62 | + .scrollable-treeview .k-animation-container { |
| 63 | + overflow: visible; |
| 64 | + } |
| 65 | +</style> |
| 66 | +
|
| 67 | +<h2>TreeView Parent Scrolling</h2> |
| 68 | +
|
| 69 | +<div class="scrollable-parent"> |
| 70 | + <TelerikTreeView Data="@FlatData" |
| 71 | + @bind-ExpandedItems="@ExpandedItems" /> |
| 72 | +</div> |
| 73 | +
|
| 74 | +<style> |
| 75 | + /* set dimensions and enable scrollability */ |
| 76 | + .scrollable-parent { |
| 77 | + width: 300px; |
| 78 | + height: 300px; |
| 79 | + overflow: auto; |
| 80 | + } |
| 81 | +
|
| 82 | + /* allow the TreeView AND its content to overflow and trigger scrolling */ |
| 83 | + .scrollable-parent .k-treeview, |
| 84 | + .scrollable-parent .k-animation-container { |
| 85 | + overflow: visible; |
| 86 | + } |
| 87 | +</style> |
| 88 | +
|
| 89 | +@code { |
| 90 | + private IEnumerable<TreeItem> FlatData { get; set; } |
| 91 | +
|
| 92 | + private IEnumerable<object> ExpandedItems { get; set; } = new List<TreeItem>(); |
| 93 | +
|
| 94 | + protected override void OnInitialized() |
| 95 | + { |
| 96 | + FlatData = LoadTreeViewData(); |
| 97 | +
|
| 98 | + ExpandedItems = FlatData.Where(x => x.HasChildren == true); |
| 99 | + } |
| 100 | +
|
| 101 | + #region TreeView data generation |
| 102 | +
|
| 103 | + private List<TreeItem> LoadTreeViewData() |
| 104 | + { |
| 105 | + List<TreeItem> items = new List<TreeItem>(); |
| 106 | +
|
| 107 | + PopulateChildren(items, null, 1); |
| 108 | +
|
| 109 | + return items; |
| 110 | + } |
| 111 | +
|
| 112 | + private int TreeLevels { get; set; } = 3; |
| 113 | + private int ItemsPerLevel { get; set; } = 2; |
| 114 | + private int IdCounter { get; set; } = 1; |
| 115 | +
|
| 116 | + private void PopulateChildren(List<TreeItem> items, int? parentId, int level) |
| 117 | + { |
| 118 | + for (int i = 1; i <= ItemsPerLevel; i++) |
| 119 | + { |
| 120 | + var itemId = IdCounter++; |
| 121 | + items.Add(new TreeItem() |
| 122 | + { |
| 123 | + Id = itemId, |
| 124 | + Text = $"Level {level} Item {i} with some additional long text that requires a scrollbar", |
| 125 | + ParentId = parentId, |
| 126 | + HasChildren = level < TreeLevels |
| 127 | + }); |
| 128 | +
|
| 129 | + if (level < TreeLevels) |
| 130 | + { |
| 131 | + PopulateChildren(items, itemId, level + 1); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +
|
| 136 | + public class TreeItem |
| 137 | + { |
| 138 | + public int Id { get; set; } |
| 139 | + public string Text { get; set; } |
| 140 | + public int? ParentId { get; set; } |
| 141 | + public bool HasChildren { get; set; } |
| 142 | + } |
| 143 | +
|
| 144 | + #endregion |
| 145 | +} |
| 146 | +```` |
0 commit comments