|
| 1 | +--- |
| 2 | +title: How to Update All Successor Tasks When Updating the Parent Task |
| 3 | +description: Learn how to update all the successor tasks when updating the parent task end time in the Telerik UI for {{ site.framework }} Gantt. |
| 4 | +type: how-to |
| 5 | +page_title: Update Dependencies When The Parent Task End is Changed - Telerik UI for {{ site.framework }} Gantt |
| 6 | +slug: gantt-update-successor-when-parent-end-changes |
| 7 | +tags: gantt, successor, dependencies, update |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +<table> |
| 14 | + <tr> |
| 15 | + <td>Product</td> |
| 16 | + <td>Progress® Telerik® UI for {{ site.framework }} Gantt</td> |
| 17 | + </tr> |
| 18 | +</table> |
| 19 | + |
| 20 | + |
| 21 | +## Description |
| 22 | + |
| 23 | +In some scenarios the start of one task is dependent on the end of another task. In such a case, if the end of the initial task changes, the start of the dependent task should also change automatically. |
| 24 | +How can I achieve this? |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +1. Handle the [`e.Events(e=>e.Save())`](https://docs.telerik.com/kendo-ui/api/javascript/ui/gantt/events/save) event of the Gantt component. |
| 29 | +1. Find the time gap between the previous and the new end time of the updated task. |
| 30 | +1. Filter the dependencies of the updated tasks and update the start and end time accordignly. |
| 31 | + |
| 32 | + |
| 33 | +```Razor |
| 34 | +@using Telerik.Scaffolders.Models.Gantt |
| 35 | +
|
| 36 | +<script> |
| 37 | + var data = [ |
| 38 | + { |
| 39 | + id: 1, |
| 40 | + orderId: 0, |
| 41 | + parentId: null, |
| 42 | + title: "Task1", |
| 43 | + start: new Date("2014/6/17 9:00"), |
| 44 | + end: new Date("2014/6/17 10:00") |
| 45 | + }, |
| 46 | + { |
| 47 | + id: 2, |
| 48 | + orderId: 1, |
| 49 | + parentId: null, |
| 50 | + title: "Task2", |
| 51 | + start: new Date("2014/6/17 9:40"), |
| 52 | + end: new Date("2014/6/17 11:00") |
| 53 | + }, |
| 54 | + { |
| 55 | + id: 3, |
| 56 | + orderId: 2, |
| 57 | + parentId: null, |
| 58 | + title: "Task3", |
| 59 | + start: new Date("2014/6/17 11:00"), |
| 60 | + end: new Date("2014/6/17 12:30") |
| 61 | + }, |
| 62 | + { |
| 63 | + id: 4, |
| 64 | + orderId: 3, |
| 65 | + parentId: null, |
| 66 | + title: "Task4", |
| 67 | + start: new Date("2014/6/17 9:00"), |
| 68 | + end: new Date("2014/6/17 11:00") |
| 69 | + }, |
| 70 | + { |
| 71 | + id: 5, |
| 72 | + orderId: 4, |
| 73 | + parentId: null, |
| 74 | + title: "Task5", |
| 75 | + start: new Date("2014/6/17 11:00"), |
| 76 | + end: new Date("2014/6/17 12:00") |
| 77 | + }, |
| 78 | + { |
| 79 | + id: 6, |
| 80 | + orderId: 5, |
| 81 | + parentId: null, |
| 82 | + title: "Task 6", |
| 83 | + start: new Date("2014/6/17 13:30"), |
| 84 | + end: new Date("2014/6/17 15:00") |
| 85 | + } |
| 86 | + ]; |
| 87 | +
|
| 88 | + var dependencies= [ |
| 89 | + { |
| 90 | + id: 1, |
| 91 | + predecessorId: 1, |
| 92 | + successorId: 2, |
| 93 | + type: 1 |
| 94 | + }, |
| 95 | + { |
| 96 | + id: 2, |
| 97 | + predecessorId: 2, |
| 98 | + successorId: 3, |
| 99 | + type: 1 |
| 100 | + }, |
| 101 | + { |
| 102 | + id: 3, |
| 103 | + predecessorId: 4, |
| 104 | + successorId: 5, |
| 105 | + type: 1 |
| 106 | + }, |
| 107 | + { |
| 108 | + id: 4, |
| 109 | + predecessorId: 3, |
| 110 | + successorId: 6, |
| 111 | + type: 1 |
| 112 | + }, |
| 113 | + ]; |
| 114 | +
|
| 115 | +</script> |
| 116 | +
|
| 117 | +@(Html.Kendo().Gantt<TaskModel, DependencyModel>() |
| 118 | + .Name("gantt") |
| 119 | + .Height(400) |
| 120 | + .DataSource("data") |
| 121 | + .DependenciesDataSource("dependencies") |
| 122 | + .Events(e => e.Save("ganttSave")) |
| 123 | + ) |
| 124 | +
|
| 125 | +<script> |
| 126 | + function ganttSave(e) { |
| 127 | + var task = e.task; |
| 128 | +
|
| 129 | + if (!e.values.end) { |
| 130 | + return; |
| 131 | + } |
| 132 | +
|
| 133 | + var startDelta = e.values.end - e.task.end; |
| 134 | +
|
| 135 | + moveTask(e.sender, task, startDelta) |
| 136 | +
|
| 137 | + e.sender.dependencies.filter(null); |
| 138 | + e.sender.dataSource.sync(); |
| 139 | + } |
| 140 | +
|
| 141 | + function moveTask(gantt, task, startDelta) { |
| 142 | +
|
| 143 | + var t = gantt.dependencies.filter({ |
| 144 | + field: "predecessorId", |
| 145 | + operator: "equals", |
| 146 | + value: task.id |
| 147 | + }); |
| 148 | +
|
| 149 | +
|
| 150 | + $(gantt.dependencies.view()).each(function (index, dependency) { |
| 151 | +
|
| 152 | + var dependentTask = gantt.dataSource.get(dependency.successorId); |
| 153 | + var dependentTaskStart = dependentTask.get("start"); |
| 154 | + var dependentTaskEnd = dependentTask.get("end"); |
| 155 | +
|
| 156 | + dependentTask.set("start", new Date(dependentTaskStart.getTime() + startDelta)); |
| 157 | + dependentTask.set("end", new Date(dependentTaskEnd.getTime() + startDelta)); |
| 158 | +
|
| 159 | + gantt.dependencies.filter(null); |
| 160 | +
|
| 161 | + var nextTask = gantt.dataSource.get(dependentTask.id); |
| 162 | +
|
| 163 | + moveTask(gantt, nextTask, startDelta) |
| 164 | +
|
| 165 | + }); |
| 166 | + } |
| 167 | +</script> |
| 168 | +
|
| 169 | +
|
| 170 | +``` |
0 commit comments