|
| 1 | +--- |
| 2 | +title: Persist Gantt TreeList after Refresh |
| 3 | +description: How to persist the width of the Gantt's treelist after refreshing its data? |
| 4 | +type: how-to |
| 5 | +page_title: How to Display Model Fields in the Gantt Tooltip? |
| 6 | +slug: gantt-kb-persist-treelist-width-after-refresh |
| 7 | +tags: gantt, blazor, treelist, width, persist |
| 8 | +res_type: kb |
| 9 | +ticketid: 1690467 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Gantt for Blazor</td> |
| 18 | + </tr> |
| 19 | + </tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +If I resize the TreeList part of the Gantt, it gets back to its initial width, after I refresh the Gantt's data. How can I persist the current TreeList width? |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +To persist the current TreeList width, follow the steps below: |
| 29 | + |
| 30 | +1. Bind the `TreeListWidth` parameter of the Gantt to a property. In the example below, the property is named `ListWidth`. |
| 31 | +2. In the methods that manipulate the data of the Gantt (`AddRootTask` and `RemoveTask`), use JavaScript interop to invoke a function. The logic in the function gets and returns the current width of the TreeList. |
| 32 | +3. Use the current width value to update the value of the `ListWidth` property, before refreshing the data by calling `.Rebind()`. |
| 33 | + |
| 34 | +````RAZOR |
| 35 | +@inject IJSRuntime JS |
| 36 | +
|
| 37 | +<TelerikButton OnClick="@AddRootTask">Add root task</TelerikButton> |
| 38 | +
|
| 39 | +<TelerikButton OnClick="@RemoveTask">Remove Task 1</TelerikButton> |
| 40 | +
|
| 41 | +<TelerikGantt @ref="@GanttRef" |
| 42 | + Data="@GanttData" |
| 43 | + @bind-View="@SelectedView" |
| 44 | + Width="1000px" |
| 45 | + TreeListWidth="@ListWidth" |
| 46 | + Height="600px" |
| 47 | + IdField="Id" |
| 48 | + ParentIdField="ParentId" |
| 49 | + OnUpdate="@UpdateItem" |
| 50 | + OnDelete="@DeleteItem"> |
| 51 | + <GanttColumns> |
| 52 | + <GanttColumn Field="Title" |
| 53 | + Expandable="true" |
| 54 | + Width="150px" |
| 55 | + Title="Task Title"> |
| 56 | + </GanttColumn> |
| 57 | + <GanttColumn Field="PercentComplete" |
| 58 | + Title="Status" |
| 59 | + Width="60px"> |
| 60 | + </GanttColumn> |
| 61 | + <GanttColumn Field="Start" |
| 62 | + Width="90px" |
| 63 | + DisplayFormat="{0:d}"> |
| 64 | + </GanttColumn> |
| 65 | + <GanttColumn Field="End" |
| 66 | + Width="90px" |
| 67 | + DisplayFormat="{0:d}"> |
| 68 | + </GanttColumn> |
| 69 | + </GanttColumns> |
| 70 | + <GanttViews> |
| 71 | + <GanttDayView></GanttDayView> |
| 72 | + <GanttWeekView></GanttWeekView> |
| 73 | + <GanttMonthView></GanttMonthView> |
| 74 | + </GanttViews> |
| 75 | +</TelerikGantt> |
| 76 | +
|
| 77 | +<script suppress-error="BL9992"> |
| 78 | + function getListSize() { |
| 79 | + var listWidth = document.querySelector('.k-gantt .k-treelist').offsetWidth; |
| 80 | + return listWidth + "px"; |
| 81 | + } |
| 82 | +</script> |
| 83 | +
|
| 84 | +@code { |
| 85 | + private TelerikGantt<FlatModel> GanttRef; |
| 86 | + private string ListWidth { get; set; } = "390px"; |
| 87 | +
|
| 88 | + private List<FlatModel> GanttData { get; set; } |
| 89 | +
|
| 90 | + private GanttView SelectedView { get; set; } = GanttView.Week; |
| 91 | +
|
| 92 | + private async Task AddRootTask() |
| 93 | + { |
| 94 | + var i = GanttData.Last().Id + 1; |
| 95 | +
|
| 96 | + GanttData.Insert(0, new FlatModel() |
| 97 | + { |
| 98 | + Id = i, |
| 99 | + ParentId = null, |
| 100 | + Title = "new task", |
| 101 | + PercentComplete = 0, |
| 102 | + Start = new DateTime(2021, 7, 5), |
| 103 | + End = new DateTime(2021, 7, 15) |
| 104 | + }); |
| 105 | +
|
| 106 | + var currentListWidth = await JS.InvokeAsync<string>("getListSize"); |
| 107 | + ListWidth = currentListWidth; |
| 108 | +
|
| 109 | + GanttRef.Rebind(); |
| 110 | + } |
| 111 | +
|
| 112 | + private async Task RemoveTask() |
| 113 | + { |
| 114 | + var taskToRemove = GanttData.FirstOrDefault(x => x.Title == "Task 1"); |
| 115 | +
|
| 116 | + GanttData.Remove(taskToRemove); |
| 117 | +
|
| 118 | + var currentListWidth = await JS.InvokeAsync<string>("getListSize"); |
| 119 | + ListWidth = currentListWidth; |
| 120 | +
|
| 121 | + GanttRef.Rebind(); |
| 122 | + } |
| 123 | +
|
| 124 | + class FlatModel |
| 125 | + { |
| 126 | + public int Id { get; set; } |
| 127 | + public int? ParentId { get; set; } |
| 128 | + public string Title { get; set; } |
| 129 | + public double PercentComplete { get; set; } |
| 130 | + public DateTime Start { get; set; } |
| 131 | + public DateTime End { get; set; } |
| 132 | + } |
| 133 | +
|
| 134 | + private int LastId { get; set; } = 1; |
| 135 | +
|
| 136 | + protected override void OnInitialized() |
| 137 | + { |
| 138 | + GanttData = new List<FlatModel>(); |
| 139 | + var random = new Random(); |
| 140 | +
|
| 141 | + for (int i = 1; i < 6; i++) |
| 142 | + { |
| 143 | + var newItem = new FlatModel() |
| 144 | + { |
| 145 | + Id = LastId, |
| 146 | + Title = "Task " + i.ToString(), |
| 147 | + Start = new DateTime(2021, 7, 5 + i), |
| 148 | + End = new DateTime(2021, 7, 11 + i), |
| 149 | + PercentComplete = Math.Round(random.NextDouble(), 2) |
| 150 | + }; |
| 151 | +
|
| 152 | + GanttData.Add(newItem); |
| 153 | + var parentId = LastId; |
| 154 | + LastId++; |
| 155 | +
|
| 156 | + for (int j = 0; j < 5; j++) |
| 157 | + { |
| 158 | + GanttData.Add(new FlatModel() |
| 159 | + { |
| 160 | + Id = LastId, |
| 161 | + ParentId = parentId, |
| 162 | + Title = " Task " + i + " : " + j.ToString(), |
| 163 | + Start = new DateTime(2021, 7, 5 + j), |
| 164 | + End = new DateTime(2021, 7, 6 + i + j), |
| 165 | + PercentComplete = Math.Round(random.NextDouble(), 2) |
| 166 | + }); |
| 167 | +
|
| 168 | + LastId++; |
| 169 | + } |
| 170 | + } |
| 171 | +
|
| 172 | + base.OnInitialized(); |
| 173 | + } |
| 174 | +
|
| 175 | + private void UpdateItem(GanttUpdateEventArgs args) |
| 176 | + { |
| 177 | + var item = args.Item as FlatModel; |
| 178 | +
|
| 179 | + var foundItem = GanttData.FirstOrDefault(i => i.Id.Equals(item.Id)); |
| 180 | +
|
| 181 | + if (foundItem != null) |
| 182 | + { |
| 183 | + foundItem.Title = item.Title; |
| 184 | + foundItem.Start = item.Start; |
| 185 | + foundItem.End = item.End; |
| 186 | + foundItem.PercentComplete = item.PercentComplete; |
| 187 | + } |
| 188 | + } |
| 189 | +
|
| 190 | + private void DeleteItem(GanttDeleteEventArgs args) |
| 191 | + { |
| 192 | + var item = GanttData.FirstOrDefault(i => i.Id.Equals((args.Item as FlatModel).Id)); |
| 193 | +
|
| 194 | + RemoveChildRecursive(item); |
| 195 | + } |
| 196 | +
|
| 197 | + private void RemoveChildRecursive(FlatModel item) |
| 198 | + { |
| 199 | + var children = GanttData.Where(i => item.Id.Equals(i.ParentId)).ToList(); |
| 200 | +
|
| 201 | + foreach (var child in children) |
| 202 | + { |
| 203 | + RemoveChildRecursive(child); |
| 204 | + } |
| 205 | +
|
| 206 | + GanttData.Remove(item); |
| 207 | + } |
| 208 | +} |
| 209 | +```` |
| 210 | + |
| 211 | +## See Also |
| 212 | + |
| 213 | +- [Gantt Overview - Telerik UI for Blazor](slug:gantt-overview) |
0 commit comments