@@ -4,7 +4,7 @@ description: How to persist the width of the Gantt's treelist after refreshing i
44type : how-to
55page_title : How to Display Model Fields in the Gantt Tooltip?
66slug : gantt-kb-persist-treelist-width-after-refresh
7- tags : gantt, blazor, treelist, width, persist
7+ tags : gantt, blazor, treelist, width
88res_type : kb
99ticketid : 1690467
1010---
@@ -82,11 +82,10 @@ To persist the current TreeList width, follow the steps below:
8282</script>
8383
8484@code {
85- private TelerikGantt<FlatModel> GanttRef;
85+ private TelerikGantt<FlatModel>? GanttRef { get; set; }
8686 private string ListWidth { get; set; } = "390px";
87-
87+ private int LastId { get; set; } = 1;
8888 private List<FlatModel> GanttData { get; set; }
89-
9089 private GanttView SelectedView { get; set; } = GanttView.Week;
9190
9291 private async Task AddRootTask()
@@ -106,7 +105,7 @@ To persist the current TreeList width, follow the steps below:
106105 var currentListWidth = await JS.InvokeAsync<string>("getListSize");
107106 ListWidth = currentListWidth;
108107
109- GanttRef.Rebind();
108+ GanttRef? .Rebind();
110109 }
111110
112111 private async Task RemoveTask()
@@ -118,25 +117,47 @@ To persist the current TreeList width, follow the steps below:
118117 var currentListWidth = await JS.InvokeAsync<string>("getListSize");
119118 ListWidth = currentListWidth;
120119
121- GanttRef.Rebind();
120+ GanttRef? .Rebind();
122121 }
123122
124- class FlatModel
123+
124+ private void UpdateItem(GanttUpdateEventArgs args)
125125 {
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; }
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+ }
132137 }
133138
134- private int LastId { get; set; } = 1;
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+ }
135157
136158 protected override void OnInitialized()
137159 {
138160 GanttData = new List<FlatModel>();
139- var random = new Random();
140161
141162 for (int i = 1; i < 6; i++)
142163 {
@@ -146,7 +167,7 @@ To persist the current TreeList width, follow the steps below:
146167 Title = "Task " + i.ToString(),
147168 Start = new DateTime(2021, 7, 5 + i),
148169 End = new DateTime(2021, 7, 11 + i),
149- PercentComplete = Math.Round(random .NextDouble(), 2)
170+ PercentComplete = Math.Round(Random.Shared .NextDouble(), 2)
150171 };
151172
152173 GanttData.Add(newItem);
@@ -162,7 +183,7 @@ To persist the current TreeList width, follow the steps below:
162183 Title = " Task " + i + " : " + j.ToString(),
163184 Start = new DateTime(2021, 7, 5 + j),
164185 End = new DateTime(2021, 7, 6 + i + j),
165- PercentComplete = Math.Round(random .NextDouble(), 2)
186+ PercentComplete = Math.Round(Random.Shared .NextDouble(), 2)
166187 });
167188
168189 LastId++;
@@ -172,38 +193,14 @@ To persist the current TreeList width, follow the steps below:
172193 base.OnInitialized();
173194 }
174195
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)
196+ class FlatModel
198197 {
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);
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; }
207204 }
208205}
209206````
0 commit comments