Skip to content

Commit dbdcc78

Browse files
Merge kb-gantt-persist-treelist-width-3061 into production (#3079)
* kb(Gantt): add Gantt TreeList width persistance kb * kb(Gantt): add PR review suggestions --------- Co-authored-by: IvanDanchev <ifujin@gmail.com>
1 parent 74ebb97 commit dbdcc78

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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
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 { get; set; }
86+
private string ListWidth { get; set; } = "390px";
87+
private int LastId { get; set; } = 1;
88+
private List<FlatModel> GanttData { get; set; }
89+
private GanttView SelectedView { get; set; } = GanttView.Week;
90+
91+
private async Task AddRootTask()
92+
{
93+
var i = GanttData.Last().Id + 1;
94+
95+
GanttData.Insert(0, new FlatModel()
96+
{
97+
Id = i,
98+
ParentId = null,
99+
Title = "new task",
100+
PercentComplete = 0,
101+
Start = new DateTime(2021, 7, 5),
102+
End = new DateTime(2021, 7, 15)
103+
});
104+
105+
var currentListWidth = await JS.InvokeAsync<string>("getListSize");
106+
ListWidth = currentListWidth;
107+
108+
GanttRef?.Rebind();
109+
}
110+
111+
private async Task RemoveTask()
112+
{
113+
var taskToRemove = GanttData.FirstOrDefault(x => x.Title == "Task 1");
114+
115+
GanttData.Remove(taskToRemove);
116+
117+
var currentListWidth = await JS.InvokeAsync<string>("getListSize");
118+
ListWidth = currentListWidth;
119+
120+
GanttRef?.Rebind();
121+
}
122+
123+
124+
private void UpdateItem(GanttUpdateEventArgs args)
125+
{
126+
var item = args.Item as FlatModel;
127+
128+
var foundItem = GanttData.FirstOrDefault(i => i.Id.Equals(item.Id));
129+
130+
if (foundItem != null)
131+
{
132+
foundItem.Title = item.Title;
133+
foundItem.Start = item.Start;
134+
foundItem.End = item.End;
135+
foundItem.PercentComplete = item.PercentComplete;
136+
}
137+
}
138+
139+
private void DeleteItem(GanttDeleteEventArgs args)
140+
{
141+
var item = GanttData.FirstOrDefault(i => i.Id.Equals((args.Item as FlatModel).Id));
142+
143+
RemoveChildRecursive(item);
144+
}
145+
146+
private void RemoveChildRecursive(FlatModel item)
147+
{
148+
var children = GanttData.Where(i => item.Id.Equals(i.ParentId)).ToList();
149+
150+
foreach (var child in children)
151+
{
152+
RemoveChildRecursive(child);
153+
}
154+
155+
GanttData.Remove(item);
156+
}
157+
158+
protected override void OnInitialized()
159+
{
160+
GanttData = new List<FlatModel>();
161+
162+
for (int i = 1; i < 6; i++)
163+
{
164+
var newItem = new FlatModel()
165+
{
166+
Id = LastId,
167+
Title = "Task " + i.ToString(),
168+
Start = new DateTime(2021, 7, 5 + i),
169+
End = new DateTime(2021, 7, 11 + i),
170+
PercentComplete = Math.Round(Random.Shared.NextDouble(), 2)
171+
};
172+
173+
GanttData.Add(newItem);
174+
var parentId = LastId;
175+
LastId++;
176+
177+
for (int j = 0; j < 5; j++)
178+
{
179+
GanttData.Add(new FlatModel()
180+
{
181+
Id = LastId,
182+
ParentId = parentId,
183+
Title = " Task " + i + " : " + j.ToString(),
184+
Start = new DateTime(2021, 7, 5 + j),
185+
End = new DateTime(2021, 7, 6 + i + j),
186+
PercentComplete = Math.Round(Random.Shared.NextDouble(), 2)
187+
});
188+
189+
LastId++;
190+
}
191+
}
192+
193+
base.OnInitialized();
194+
}
195+
196+
class FlatModel
197+
{
198+
public int Id { get; set; }
199+
public int? ParentId { get; set; }
200+
public string Title { get; set; }
201+
public double PercentComplete { get; set; }
202+
public DateTime Start { get; set; }
203+
public DateTime End { get; set; }
204+
}
205+
}
206+
````
207+
208+
## See Also
209+
210+
- [Gantt Overview - Telerik UI for Blazor](slug:gantt-overview)

0 commit comments

Comments
 (0)