TaskModel
This class represents a task in your Gantt project. Extend it to add your own custom task fields and methods.
Subclassing the TaskModel class
To subclass the TaskModel and add extra fields and API methods, please see the snippet below.
class MyTaskModel extends TaskModel {
static get fields() {
return [
{ name: 'importantDate', type: 'date' }
]
}
After creating your own Task model class, configure the taskModelClass on Project to use it:
new Gantt({
project : {
taskModelClass : MyTaskModel
}
});
Creating a new Task programmatically
To create a new task programmatically, simply call the TaskModel constructor and pass in any field values.
const newTask = new TaskModel({
name : 'My awesome task',
importantDate : new Date(2022, 0, 1),
percentDone : 80 // So awesome it's almost done
// ...
});
Async scheduling
A record created from an TaskModel is normally part of a TaskStore, which in turn is part of a project. When dates or the duration of a task is changed, the project performs async calculations of the other related fields (including the field of other tasks affected by the change). For example if duration is changed, it will recalculate endDate.
As a result of this being an async operation, the values of other fields are not guaranteed to be up to date
immediately after a change. To ensure data is up to date, await the calculations to finish.
For example, endDate is not up to date after this operation:
taskRecord.duration = 5;
// taskRecord.endDate not yet calculated
But if calculations are awaited it is up to date:
taskRecord.duration = 5;
await taskRecord.project.commitAsync();
// endDate is calculated
In case of multiple changes no need to trigger recalculation after each of them:
// change taskRecord1 start and duration
taskRecord1.startDate = '2021-11-15';
taskRecord1.duration = 5;
// change taskRecord2 duration
taskRecord2.duration = 1;
// change taskRecord3 finish date
taskRecord3.endDate = '2021-11-17';
// now when all changes are done trigger rescheduling
await taskRecord.project.commitAsync();
Manually vs automatically scheduled tasks
A task can be either automatically (default) or manually scheduled. This is defined by the manuallyScheduled field. Manually scheduled tasks are not affected by the automatic scheduling process, which means their start/end dates are meant to be changed by user manually. Such tasks are not shifted by their predecessors nor such summary tasks rollup their children start/end dates. While automatically scheduled tasks start/end dates are calculated by the Gantt.
Start and end dates
For all tasks, the end date is non-inclusive: startDate <= date < endDate. Example: a task which starts at 2020/07/18 and has 2 days duration, should have the end date: 2020/07/20, not 2018/07/19 23:59:59. The start and end dates of tasks in are points on the time axis and if you specify that a task starts 01/01/2020 and has 1 day duration, that means the start point is 01/01/2020 00:00 and end point is 02/01/2020 00:00.
Configs
1
Configs
1Properties
97
Properties
97Class hierarchy
Other
Returns all dependencies of this task (both incoming and outgoing)
An array of the assignments, related to this task
The event first segment or null if the event is not segmented.
Property which encapsulates the effort's magnitude and units.
UI fields representing this property are disabled for summary events. See isEditable for details.
A Set<Gantt.model.DependencyModel> of the incoming dependencies for this task
The event last segment or null if the event is not segmented.
A Set<Gantt.model.DependencyModel> of the outgoing dependencies for this task
Returns all predecessor dependencies of this task
Returns all direct predecessor tasks of a task
Returns count of all sibling nodes (including their children).
Returns all resources assigned to an event.
Returns the sequential number of the task. A sequential number means the ordinal position of the task in the total dataset, regardless of its nesting level and collapse/expand state of any parent tasks. The root node has a sequential number equal to 0.
For example, in the following tree data sample sequential numbers are specified in the comments:
root : {
children : [
{ // 1
leaf : true
},
{ // 2
children : [
{ // 3
children : [
{ // 4
leaf : true
},
{ // 5
leaf : true
}
]
}]
},
{ // 6
leaf : true
}
]
}
If we collapse parent tasks, sequential number of collapsed tasks won't change.
Returns all successor dependencies of this task
Returns all direct successor tasks of a task
Parent & children
This static configuration option allows you to control whether an empty parent task should be converted into a leaf. Enable/disable it for a whole class:
TaskModel.convertEmptyParentToLeaf = false;
By specifying true, all empty parents will be considered leafs. Can also be assigned a configuration object
with the following Boolean properties to customize the behaviour:
onLoad- Apply the transformation on load to any parents without children (children : [])onRemove- Apply the transformation when all children have been removed from a parent
TaskModel.convertEmptyParentToLeaf = {
onLoad : false,
onRemove : true
}
Scheduling
Returns the task duration in milliseconds.
A read only alias for postponedConflict field.
Editing
JSON
Models & Stores
Progress
Functions
86
Functions
86Other
This method assigns a resource to this task.
Will cause the schedule to be updated - returns a Promise
| Parameter | Type | Description |
|---|---|---|
resource | ResourceModel | The instance of ResourceModel |
units | Number | The |
rateTable | ResourceRateTableModel | String | Number | Resource rate table to use for the assignment |
cost | Number | The assignment cost, This is only used when assigning a cost-type resource. |
Indicates whether the task can perform the provided duration conversion. The task might not be able to convert units on early stages of data loading when the project has not loaded its conversion rates yet. So the method by default checks that the project has loaded the rates needed for conversion.
| Parameter | Type | Description |
|---|---|---|
duration | Number | Duration value |
fromUnit | DurationUnit | Duration value time unit |
toUnit | DurationUnit | Target time unit to convert the value to |
true is the conversion is possible and false otherwise.
Propagates changes to the dependent tasks. For example:
// double a task duration
task.duration *= 2;
// call commitAsync() to do further recalculations caused by the duration change
task.commitAsync().then(() => console.log('Schedule updated'));
Converts a duration value from one time unit to another. The method is a generator since it embeds into the Engine scheduling mechanics.
| Parameter | Type | Description |
|---|---|---|
duration | Number | Duration value |
fromUnit | DurationUnit | Duration value time unit |
toUnit | DurationUnit | Target time unit to convert the value to |
The converted duration value.
Converts this task to a milestone (start date will match the end date).
Converts the milestone task to a regular task with a duration of 1 (keeping current durationUnit).
If given resource is assigned to this task, returns a AssignmentModel record.
Otherwise returns null
| Parameter | Type | Description |
|---|---|---|
resource | ResourceModel | The instance of ResourceModel |
Returns the baseline of the provided version (1-based index).
// return the baseline version 3
task.getBaseline(3);
| Parameter | Type | Description |
|---|---|---|
version | Number | The baseline version to retrieve. |
The requested baseline.
Returns the event ignoreResourceCalendar field value.
The event ignoreResourceCalendar field value.
Returns the task's planned percent done. To calculate it, task needs to have at least 1 baseline set and the reference "status" date should be provided either to this method, or to the project.
The calculation is performed as follows:
- If the baseline's end date is before the status date, result value is
100% - If the baseline's start date is after the status date, result value is
0% - Now the status date is in between the baseline's start and end date, result value is: (status date - baseline start date) / (baseline end date - baseline start date). Here the "minus" operation effectively calculates the duration between the two moments on timeaxis. Duration calculation takes into account the task's calendar.
| Parameter | Type | Description |
|---|---|---|
statusDate | Date | Date to calculate the % complete for. If not provided, the project's statusDate is used. |
baselineVersion | Number | The baseline index (1-based) to use for calculating. If not provided, value |
Planned % complete of the task.
Defines if the given task field should be manually editable in UI. You can override this method to provide your own logic.
By default, the method defines:
- earlyStartDate, earlyEndDate, lateStartDate, lateEndDate, totalSlack as not editable;
- effort, fullEffort, percentDone as not editable for summary tasks;
- endDate, duration and fullDuration fields as not editable for summary tasks except the manually scheduled ones.
| Parameter | Type | Description |
|---|---|---|
fieldName | String | Name of the field |
Returns true if the field is editable, false if it is not and undefined if the task has
no such field.
Merges the task segments. The method merges two provided task segments (and all the segment between them if any).
| Parameter | Type | Description |
|---|---|---|
segment1 | EventSegmentModel | First segment to merge. |
segment2 | EventSegmentModel | Second segment to merge. |
Refreshes the wbsValue of this record and its children. This is rarely needed but may be required
after a complex series of filtering, inserting, or removing nodes. In particular, removing nodes does create a
gap in wbsValue values that may be undesirable.
| Parameter | Type | Description |
|---|---|---|
options | Object | A set of options for refreshing. |
options.deep | Boolean | Pass |
options.silent | Boolean | Pass |
options.useOrderedTree | Boolean | Pass |
index | Number | The index of this node in its parent's children array. Pass -1 to ignore this node's
|
Starts a postponed conflict resolution (if task has any) and then calls commitAsync method and returns its result.
Please refer to allowPostponedConflicts config documentation for details.
Applies the start/end dates from the task to the corresponding baseline.
const task = new TaskModel({
name: 'New task',
startDate: '2019-01-14',
endDate: '2019-01-17',
duration: 3,
baselines: [
// Baseline version 1
{
startDate: '2019-01-13',
endDate: '2019-01-16'
},
// Baseline version 2
{
startDate: '2019-01-14',
endDate: '2019-01-17'
},
// Baseline version 3
{
startDate: '2019-01-15',
endDate: '2019-01-18'
}
]
});
// Apply the task's start/end dates to the baseline version 3
task.setBaseline(3);
| Parameter | Type | Description |
|---|---|---|
version | Number | The baseline version to update |
Sets the calendar of the task. Will cause the schedule to be updated - returns a Promise
| Parameter | Type | Description |
|---|---|---|
calendar | CalendarModel | The new calendar. Provide |
Sets the constraint type and (optionally) constraining date to the task.
| Parameter | Type | Description |
|---|---|---|
constraintType | finishnoearlierthan | finishnolaterthan | mustfinishon | muststarton | startnoearlierthan | startnolaterthan | null | Constraint type, please refer to the constraintType for the valid values. |
constraintDate | Date | Constraint date. |
Updates the duration (and optionally unit) of the task. Will cause the schedule to be updated - returns a Promise
| Parameter | Type | Description |
|---|---|---|
duration | Number | New duration value |
unit | DurationUnit | New duration unit |
Updates the effort (and optionally unit) of the task. Will cause the schedule to be updated - returns a Promise
| Parameter | Type | Description |
|---|---|---|
effort | Number | New effort value |
unit | DurationUnit | New effort unit |
Sets the end date of the task. Will cause the schedule to be updated - returns a Promise
Note, that the actually set end date may be adjusted, according to the calendar, by skipping the non-working time backward.
| Parameter | Type | Description |
|---|---|---|
date | Date | The new end date. |
keepDuration | Boolean | Whether to keep the duration (and update the start date), while changing the end date, or vice-versa. |
Sets the task ignoreResourceCalendar field value and triggers rescheduling.
| Parameter | Type | Description |
|---|---|---|
ignore | Boolean | Provide |
Either activates or deactivates the task depending on the passed value.
Will cause the schedule to be updated - returns a Promise
| Parameter | Type | Description |
|---|---|---|
inactive | Boolean |
|
Sets segments field value.
| Parameter | Type | Description |
|---|---|---|
segments | EventSegmentModel[] | Array of segments or null to make the task not segmented. |
Sets the start date of the task. Will cause the schedule to be updated - returns a Promise
Note, that the actually set start date may be adjusted, according to the calendar, by skipping the non-working time forward.
| Parameter | Type | Description |
|---|---|---|
date | Date | The new start date. |
keepDuration | Boolean | Whether to keep the duration (and update the end date), while changing the start date, or vice-versa. |
Splits the task to segments.
| Parameter | Type | Description |
|---|---|---|
from | Date | The date to split this task at. |
lag | Number | Split duration. |
lagUnit | String | Split duration unit. |
This method unassigns a resource from this task.
Will cause the schedule to be updated - returns a Promise
| Parameter | Type | Description |
|---|---|---|
resource | ResourceModel | The instance of ResourceModel |
Configuration
Editing
Events
Misc
Parent & children
Typedefs
3
Typedefs
3Options for the convertEmptyParentToLeaf static property.
| Parameter | Type | Description |
|---|---|---|
onLoad | Boolean |
|
onRemove | Boolean |
|
| Parameter | Type | Description |
|---|---|---|
kind | own | enforced | inherited | The type of the direction value. |
direction | Forward | Backward | The actual direction. Depending on the |
enforcedBy | TaskModel | The task which forces the current direction |
inheritedFrom | TaskModel | The task from which the current direction is inherited |
Fields
47
Fields
47Common
The scheduling direction of this event. The Forward direction corresponds to the as-soon-as-possible scheduling (ASAP),
Backward - to as-late-as-possible (ALAP). The ASAP tasks "sticks" to the project's start date,
and ALAP tasks - to the project's end date.
If not specified (which is the default), direction is inherited from the parent task (and from the project for top-level tasks). By default, the project model has forward scheduling mode.
Note The ALAP-scheduled task in the ASAP-scheduled project will turn all of its successors into ALAP-scheduled tasks, even if their scheduling direction is specified explicitly by the user as ASAP. We can say that ALAP-scheduling is propagated down through the successors chain. This propagation, however, will stop in the following cases:
- If a successor is manually scheduled
- If a successor has a "Must start/finish on" constraint
- If a dependency to successor is inactive
Similarly, the ASAP-scheduled task in the ALAP-scheduled project will turn all of its predecessors into ASAP-scheduled tasks (also regardless of the user-provided value).
When such propagation is in action, the value of this field is ignored and the UI will disable controls for it.
To determine the actual scheduling direction of the task (which might be different from the user-provided value), one can use the effectiveDirection field.
Note For the purposes of compatibility with MS Project and to ease the migration process for users, by default, scheduling direction can be set using the "Constraint type" field on the "Advanced" tab of the task editor. The forward scheduling is specified in it as "As soon as possible" option and backward - "As late as possible". One can also disable the includeAsapAlapAsConstraints config to render a separate "Scheduling direction" field.
The calculated effective scheduling direction of this event. See the direction field for details.
Unique identifier of task (mandatory)
Name of the task
A freetext note about the task.
Features
Set this to true to roll up a task to its closest parent
Set this to true if this task should be shown in the Timeline widget
Interaction
Specify false to prevent the event from being dragged (if TaskDrag feature is used)
Specify false to prevent the task from being resized (if TaskResize feature is used). You can also specify 'start' or 'end' to only allow resizing in one direction
Other
Changes task's background color. Named colors are applied as a b-color-{color} (for example
b-color-red) CSS class to the task's bar. Colors specified as hex, rgb() etc. are applied as a
--b-primary CSS variable to the bar.
If no color is specified, any color defined in Gantt's eventColor config will apply instead.
For available standard colors, see EventColor.
Using named colors:
const gantt = new Gantt({
project {
tasks : [
{ id : 1, name : 'Important task', eventColor : 'red' }
]
}
});
Will result in:
<div class="b-gantt-task-wrap b-color-red">
Using non-named colors:
const gantt = new Gantt({
project {
tasks : [
{ id : 1, name : 'Important task', eventColor : '#ff0000' }
]
}
});
Will result in:
<div class="b-gantt-task-wrap" style="--b-primary: #ff0000">
Parent & children
Child nodes. To allow loading children on demand, specify children : true in your data. Omit the field
for leaf tasks.
Note, if the task store loads data from a remote origin, make sure readUrl is specified, and optionally parentIdParamName is set, otherwise loadChildren has to be implemented.
Scheduling
Shows the amount of effort that has already been done by resources assigned to the task.
For a summary task the value is calculated as sum of children actual effort values. Otherwise if the task has assignments the value is calculated as sum of assignments actual efforts. And if not the value is calculated as effort multiplied by percentDone value.
The calendar, assigned to the task. Allows you to set the time when task can be performed.
Field defining the constraint boundary date or null if constraintType is null.
Field storing the task constraint alias or null if not constraint set.
Valid values are:
- "finishnoearlierthan"
- "finishnolaterthan"
- "mustfinishon"
- "muststarton"
- "startnoearlierthan"
- "startnolaterthan"
The total projected cost for the event.
A calculated field indicating if the task is critical. A task considered critical if its delaying causes the project delay. The field value is calculated based on totalSlack field value.
if (task.critical) {
Toast.show(`The ${task.name} is critical!`);
}
A deadline date for this task. Does not affect scheduling logic.
Note that the field always returns a Date.
The numeric part of the task duration (the number of units).
UI fields representing this data field are disabled for summary events except the manually scheduled events. See isEditable for details.
The unit part of the task duration, defaults to "day" (days). Valid values are:
| Unit | Description |
|---|---|
millisecond |
Milliseconds |
second |
Seconds |
minute |
Minutes |
hour |
Hours |
day |
Days |
week |
Weeks |
month |
Months |
quarter |
Quarters |
year |
Years |
It also supports e* (elapsed) duration units, where tasks run continuously
24 hours a day, 7 days a week, ignoring all non-working time, ideal for tasks
that need to run without breaks or for rough project planning with limited date/duration info.
| Unit | Description |
|---|---|
eminute |
Elapsed minute. Lasts 60 seconds. |
ehour |
Elapsed hours. Lasts 60 minutes. |
eday |
Elapsed day. Lasts 24 hours. |
eweek |
Elapsed week. Lasts 7 elapsed days. |
emonth |
Elapsed month. Lasts 30 elapsed days. |
equarter |
Elapsed quarter. Lasts 3 elapsed months. |
eyear |
Elapsed year. Lasts 3 elapsed quarters or 12 elapsed months. |
For example:
new TaskModel({
startDate : '2024-09-10',
endDate : '2024-09-17',
duration : 7,
durationUnit : 'ed'
});
This field is readonly after creation, to change it use the setDuration call.
A calculated field storing the early end date of the task. The early end date is the earliest possible date the task can finish. This value is calculated based on the earliest dates of the task predecessors and the task own constraints. If the task has no predecessors nor other constraints, its early end date matches the project start date plus the task duration.
UI fields representing this data field are naturally disabled since the field is readonly. See isEditable for details.
A calculated field storing the early start date of the task. The early start date is the earliest possible date the task can start. This value is calculated based on the earliest dates of the task predecessors and the task own constraints. If the task has no predecessors nor other constraints, its early start date matches the project start date.
UI fields representing this data field are naturally disabled since the field is readonly. See isEditable for details.
The numeric part of the task effort (the number of units). The effort of the "parent" tasks will be automatically set to the sum of efforts of their "child" tasks
UI fields representing this data field are disabled for summary events. See isEditable for details.
This boolean flag defines what part of task data should be updated in the FixedUnits scheduling mode.
If it is true, then effort is kept intact, and duration is updated. If it is false -
vice-versa.
The unit part of the task's effort, defaults to "h" (hours). Valid values are:
- "millisecond" - Milliseconds
- "second" - Seconds
- "minute" - Minutes
- "hour" - Hours
- "day" - Days
- "week" - Weeks
- "month" - Months
- "quarter" - Quarters
- "year"- Years
This field is readonly after creation, to change it use the setEffort call.
End date of the task in ISO 8601 format
UI fields representing this data field are disabled for summary events except the manually scheduled events. See isEditable for details.
Note that the field always returns a Date.
When set to true the calendars of the assigned resources
are not taken into account when scheduling the task.
By default the field value is false resulting in that the task performs only when
its own calendar and some of the assigned
resource calendars allow that.
When set to true the task becomes inactive and stops taking part in the project scheduling (doesn't
affect linked tasks, rolls up its attributes and affect its assigned resources allocation).
A calculated field storing the late end date of the task. The late end date is the latest possible date the task can finish. This value is calculated based on the latest dates of the task successors and the task own constraints. If the task has no successors nor other constraints, its late end date matches the project end date.
UI fields representing this data field are naturally disabled since the field is readonly. See isEditable for details.
A calculated field storing the late start date of the task. The late start date is the latest possible date the task can start. This value is calculated based on the latest dates of the task successors and the task own constraints. If the task has no successors nor other constraints, its late start date matches the project end date minus the task duration.
UI fields representing this data field are naturally disabled since the field is readonly. See isEditable for details.
When set to true, the startDate of the task will not be changed by any of its incoming
dependencies or constraints.
The current status of a task, expressed as the percentage completed (integer from 0 to 100)
UI fields representing this data field are disabled for summary events. See isEditable for details.
The field contains a postponed unresolved scheduling conflict the task (if any).
The conflict postponing functionality can be activated with the allowPostponedConflicts and autoPostponeConflicts configuration options, Please refer to those docs for additional details.
Specifies how the task should treat the project border (the project start or end depending if it's scheduled forward or backwards respectively).
The task can either respect the project border which for example means it cannot be placed before its forward scheduled project start. Or the task can ignore the project border and be scheduled regardless of that constraint.
Possible values are:
honor- task respects the project border.ignore- task ignores the project border.conflict- the project triggers schedulingConflict event when the task attempts to violate its border. So if Gantt has displaySchedulingIssueResolutionPopup enabled it will display a popup asking user to choose an appropriate resolution. If the option is disabled the application can track the event and implement some other way to handle the conflict.
This field defines the scheduling mode for the task. Based on this field some fields of the task will be "fixed" (should be provided by the user) and some - computed.
Possible values are:
-
Normalis the default (and backward compatible) mode. It means the task will be scheduled based on information about its start/end dates, task own calendar (project calendar if there's no one) and calendars of the assigned resources. -
FixedDurationmode means, that task has fixed start and end dates, but its effort will be computed dynamically, based on the assigned resources information. Typical example of such task is - meeting. Meetings typically have pre-defined start and end dates and the more people are participating in the meeting, the more effort is spent on the task. When duration of such task increases, its effort is increased too (and vice-versa). Note: fixed start and end dates here doesn't mean that a user can't update them via GUI, the only field which won't be editable in GUI is the effort field, it will be calculated according to duration and resources assigned to the task. -
FixedEffortmode means, that task has fixed effort and computed duration. The more resources will be assigned to this task, the less the duration will be. The typical example will be a "paint the walls" task - several painters will complete it faster. -
FixedUnitsmode means, that the assignment level of all assigned resources will be kept as provided by the user, and either effort or duration of the task is recalculated, based on the effortDriven flag.
Segments of the task that appear when the task gets splitToSegments.
A calculated field storing unit for the totalSlack value.
Start date of the task in ISO 8601 format
UI fields representing this data field are disabled for summary events except the manually scheduled events. See isEditable for details.
Note that the field always returns a Date.
A calculated field storing the total slack (or total float) of the task. The total slack is the amount of working time the task can be delayed without causing a delay to the project end. The value is expressed in slackUnit units.
// let output slack info to the console
console.log(`The ${task.name} task can be delayed for ${task.totalSlack} ${slackUnit}s`)
UI fields representing this data field are naturally disabled since the field is readonly. See isEditable for details.
This field is automatically set to true when the task is "unscheduled" - user has provided an empty
string in one of the UI editors for start date, end date or duration. Such task is not rendered,
and does not affect the schedule of its successors.
To schedule the task back, enter one of the missing values, so that there's enough information to calculate start date, end date and duration.
Note, that setting this field manually does nothing. This field should be persisted, but not updated manually.
The WBS for this task record. This field is automatically calculated and maintained by the store. This calculation can be refreshed by calling refreshWbs.
To get string representation of the WBS value (e.g. '2.1.3'), use value property.
Styling
An encapsulation of the CSS classes to be added to the rendered event element.
Always returns a DomClassList, but may still be treated as a string. For granular control of adding and removing individual classes, it is recommended to use the DomClassList API.
CSS class specifying an icon to apply to the task row
CSS class specifying an icon to apply to the task bar