TaskDrag
Allows user to drag and drop tasks within Gantt, to change their start date.
Constraining the drag drop area
You can constrain how the dragged task is allowed to move by using getDateConstraints. This method is configured on the Gantt instance and lets you define the date range for the dragged task programmatically.
Drag drop tasks from outside
Dragging unplanned tasks from an external grid is a very popular use case. Please refer to the Drag from grid demo and study the Drag from grid guide to learn more.
Validating a drag drop operation
It is easy to programmatically decide what is a valid drag drop operation. Use the validatorFn
and return either true / false (optionally a message to show to the user).
features : {
taskDrag : {
validatorFn(draggedTaskRecords, newStartDate) {
const valid = Date.now() >= newStartDate;
return {
valid,
message : valid ? '' : 'Not allow to drag a task into the past'
};
}
}
}
If you instead want to do a single validation upon drop, you can listen to beforeTaskDropFinalize
and set the valid flag on the context object provided.
const gantt = new Gantt({
listeners : {
beforeTaskDropFinalize({ context }) {
const { taskRecords } = context;
// Don't allow dropping a task in the past
context.valid = Date.now() <= eventRecords[0].startDate;
}
}
});
Preventing drag of certain tasks
To prevent certain tasks from being dragged, you have two options. You can set draggable
to false in your data, or you can listen for the beforeTaskDrag event and
return false to block the drag.
new Gantt({
listeners : {
beforeTaskDrag({ taskRecord }) {
// Only allow dragging tasks that has not started
return taskRecord.percentDone === 0;
}
}
})
Customizing the drag drop tooltip
To show custom HTML in the tooltip, please see the tooltipTemplate config. Example:
features: {
taskDrag: {
// A minimal start date tooltip
tooltipTemplate : ({ taskRecord, startDate }) => {
return DateHelper.format(startDate, 'HH:mm');
}
}
}
This feature is enabled by default
Configs
21
Configs
21Other
Set to true to drag all selected tasks. Set to false to only drag one task at a time
Set to true to enable dragging task while pinning dependent tasks. By default, this behavior is activated
if you hold CTRL key during drag. Alternatively, you may provide key name to use. Supported values are:
- 'ctrl'
- 'shift'
- 'alt'
- 'meta'
Note: Only supported in forward-scheduled project
Template used to generate drag tooltip contents.
const gantt = new Gantt({
features : {
taskDrag : {
tooltipTemplate({taskRecord, startText}) {
return `${taskRecord.name}: ${startText}`
}
}
}
});
| Parameter | Type | Description |
|---|---|---|
data | Object | Tooltip data |
data.taskRecord | TaskModel | |
data.valid | Boolean | Currently over a valid drop target or not |
data.startDate | Date | New start date |
data.endDate | Date | New end date |
An empty function by default, but provided so that you can perform custom validation on the item being dragged. This function is called during the drag and drop process and also after the drop is made. Return true if the new position is valid, false to prevent the drag.
| Parameter | Type | Description |
|---|---|---|
taskRecords | TaskModel[] | An array of tasks being dragged |
startDate | Date | The new start date |
duration | Number | The duration of the item being dragged |
event | Event | The event object |
true if this validation passes, false if it does not.
Or an object with 2 properties: valid - Boolean true/false depending on validity,
and message - String with a custom error message to display when invalid.
this reference for the validatorFn
Misc
Properties
22
Properties
22Common
Class hierarchy
Other
Set to true to drag all selected tasks. Set to false to only drag one task at a time
Gets or sets special key to activate successor pinning behavior. Supported values are:
- 'ctrl'
- 'shift'
- 'alt'
- 'meta'
Assign false to disable it.
Functions
29
Functions
29Configuration
Events
Misc
Other
Events
11
Events
11Fires on the owning Gantt after a task drop, regardless if the drop validity
// Adding a listener using the "on" method
taskDrag.on('afterTaskDrop', ({ source, taskRecords, valid }) => {
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecords | TaskModel[] | |
valid | Boolean |
Fires on the owning Gantt before task dragging starts. Return false to prevent the action.
// Adding a listener using the "on" method
taskDrag.on('beforeTaskDrag', ({ source, taskRecord, event }) => {
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecord | TaskModel | |
event | Event | The native browser event |
Fires on the owning Gantt to allow implementer to prevent immediate finalization by setting data.context.async = true
in the listener, to show a confirmation popup etc
gantt.on('beforetaskdropfinalize', ({ context }) => {
context.async = true;
setTimeout(() => {
// async code don't forget to call finalize
context.finalize();
}, 1000);
})
// Adding a listener using the "on" method
taskDrag.on('beforeTaskDropFinalize', ({ source, context, context.taskRecords, context.valid, context.async, context.finalize }) => {
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | Gantt instance |
context | Object | |
context.taskRecords | TaskModel[] | The dragged task records |
context.valid | Boolean | Set this to |
context.async | Boolean | Set true to handle dragdrop asynchronously (e.g. to wait for user confirmation) |
context.finalize | function | Call this method to finalize dragdrop. This method accepts one argument: pass true to update records, or false, to ignore changes |
Fires on the owning Gantt while a task is being dragged
// Adding a listener using the "on" method
taskDrag.on('taskDrag', ({ source, taskRecords, startDate, endDate, dragData, changed }) => {
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecords | TaskModel[] | |
startDate | Date | |
endDate | Date | |
dragData | Object | |
changed | Boolean |
|
Fires on the owning Gantt when task dragging starts
// Adding a listener using the "on" method
taskDrag.on('taskDragStart', ({ source, taskRecords }) => {
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecords | TaskModel[] |
Fires on the owning Gantt after a valid task drop
// Adding a listener using the "on" method
taskDrag.on('taskDrop', ({ source, taskRecords, isCopy }) => {
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecords | TaskModel[] | |
isCopy | Boolean |
Event handlers
11
Event handlers
11Called on the owning Gantt after a task drop, regardless if the drop validity
new TaskDrag({
onAfterTaskDrop({ source, taskRecords, valid }) {
}
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecords | TaskModel[] | |
valid | Boolean |
Called on the owning Gantt before task dragging starts. Return false to prevent the action.
new TaskDrag({
onBeforeTaskDrag({ source, taskRecord, event }) {
}
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecord | TaskModel | |
event | Event | The native browser event |
Called on the owning Gantt to allow implementer to prevent immediate finalization by setting data.context.async = true
in the listener, to show a confirmation popup etc
gantt.on('beforetaskdropfinalize', ({ context }) => {
context.async = true;
setTimeout(() => {
// async code don't forget to call finalize
context.finalize();
}, 1000);
})
new TaskDrag({
onBeforeTaskDropFinalize({ source, context }) {
}
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | Gantt instance |
context | Object | |
context.taskRecords | TaskModel[] | The dragged task records |
context.valid | Boolean | Set this to |
context.async | Boolean | Set true to handle dragdrop asynchronously (e.g. to wait for user confirmation) |
context.finalize | function | Call this method to finalize dragdrop. This method accepts one argument: pass true to update records, or false, to ignore changes |
Called on the owning Gantt while a task is being dragged
new TaskDrag({
onTaskDrag({ source, taskRecords, startDate, endDate, dragData, changed }) {
}
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecords | TaskModel[] | |
startDate | Date | |
endDate | Date | |
dragData | Object | |
changed | Boolean |
|
Called on the owning Gantt when task dragging starts
new TaskDrag({
onTaskDragStart({ source, taskRecords }) {
}
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecords | TaskModel[] |
Called on the owning Gantt after a valid task drop
new TaskDrag({
onTaskDrop({ source, taskRecords, isCopy }) {
}
});| Parameter | Type | Description |
|---|---|---|
source | Gantt | |
taskRecords | TaskModel[] | |
isCopy | Boolean |
Typedefs
2
Typedefs
2| Parameter | Type | Description |
|---|---|---|
valid | Boolean |
|
message | String | Validation message |