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

Common

disabledInstancePlugin
listenersEvents

Other

Set to true to drag all selected tasks. Set to false to only drag one task at a time

pinSuccessors: Boolean | String= falseAlso a property

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

tooltipTemplate: function

Template used to generate drag tooltip contents.

const gantt = new Gantt({
    features : {
        taskDrag : {
            tooltipTemplate({taskRecord, startText}) {
                return `${taskRecord.name}: ${startText}`
            }
        }
    }
});
ParameterTypeDescription
dataObject

Tooltip data

data.taskRecordTaskModel
data.validBoolean

Currently over a valid drop target or not

data.startDateDate

New start date

data.endDateDate

New end date

Returns: String
validatorFn: function

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.

ParameterTypeDescription
taskRecordsTaskModel[]

An array of tasks being dragged

startDateDate

The new start date

durationNumber

The duration of the item being dragged

eventEvent

The event object

Returns: Boolean | ValidationMessage -

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

showTooltipDragBase
tipDragBase

Misc

clientInstancePlugin
localeClassLocalizable
localizableLocalizable

Properties

22

Common

disabledInstancePlugin

Class hierarchy

isTaskDrag: Boolean= truereadonly
Identifies an object as an instance of TaskDrag class, or subclass thereof.
isTaskDrag: Boolean= truereadonlystatic
Identifies an object as an instance of TaskDrag class, or subclass thereof.
isDragBaseDragBase
isEventsEvents
isInstancePluginInstancePlugin
isLocalizableLocalizable

Other

Set to true to drag all selected tasks. Set to false to only drag one task at a time

pinSuccessors: Boolean | StringAlso a config

Gets or sets special key to activate successor pinning behavior. Supported values are:

  • 'ctrl'
  • 'shift'
  • 'alt'
  • 'meta'

Assign false to disable it.

isDraggingDragBase
showTooltipDragBase
tipDragBase

Lifecycle

configBase

Misc

clientInstancePlugin
localeHelperLocalizable
localeManagerLocalizable

Functions

29

Configuration

applyDefaultsstaticBase

Events

Lifecycle

destroystaticBase

Misc

doDisableInstancePlugin
initClassstaticBase
isOfTypeNamestaticBase
mixinstaticBase
optionalLstaticLocalizable

Other

getTipHtmlDragBase
LstaticLocalizable
onEvents
relayAllEvents
triggerEvents
unEvents

Events

11

Fires 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 }) => {

});
ParameterTypeDescription
sourceGantt
taskRecordsTaskModel[]
validBoolean

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 }) => {

});
ParameterTypeDescription
sourceGantt
taskRecordTaskModel
eventEvent

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 }) => {

});
ParameterTypeDescription
sourceGantt

Gantt instance

contextObject
context.taskRecordsTaskModel[]

The dragged task records

context.validBoolean

Set this to false to mark the drop as invalid

context.asyncBoolean

Set true to handle dragdrop asynchronously (e.g. to wait for user confirmation)

context.finalizefunction

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 }) => {

});
ParameterTypeDescription
sourceGantt
taskRecordsTaskModel[]
startDateDate
endDateDate
dragDataObject
changedBoolean

true if startDate has changed.

Fires on the owning Gantt when task dragging starts

// Adding a listener using the "on" method
taskDrag.on('taskDragStart', ({ source, taskRecords }) => {

});
ParameterTypeDescription
sourceGantt
taskRecordsTaskModel[]

Fires on the owning Gantt after a valid task drop

// Adding a listener using the "on" method
taskDrag.on('taskDrop', ({ source, taskRecords, isCopy }) => {

});
ParameterTypeDescription
sourceGantt
taskRecordsTaskModel[]
isCopyBoolean
catchAllEvents
destroyEvents
disableInstancePlugin
enableInstancePlugin

Event handlers

11

Called on the owning Gantt after a task drop, regardless if the drop validity

new TaskDrag({
    onAfterTaskDrop({ source, taskRecords, valid }) {

    }
});
ParameterTypeDescription
sourceGantt
taskRecordsTaskModel[]
validBoolean

Called on the owning Gantt before task dragging starts. Return false to prevent the action.

new TaskDrag({
    onBeforeTaskDrag({ source, taskRecord, event }) {

    }
});
ParameterTypeDescription
sourceGantt
taskRecordTaskModel
eventEvent

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 }) {

    }
});
ParameterTypeDescription
sourceGantt

Gantt instance

contextObject
context.taskRecordsTaskModel[]

The dragged task records

context.validBoolean

Set this to false to mark the drop as invalid

context.asyncBoolean

Set true to handle dragdrop asynchronously (e.g. to wait for user confirmation)

context.finalizefunction

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 }) {

    }
});
ParameterTypeDescription
sourceGantt
taskRecordsTaskModel[]
startDateDate
endDateDate
dragDataObject
changedBoolean

true if startDate has changed.

Called on the owning Gantt when task dragging starts

new TaskDrag({
    onTaskDragStart({ source, taskRecords }) {

    }
});
ParameterTypeDescription
sourceGantt
taskRecordsTaskModel[]

Called on the owning Gantt after a valid task drop

new TaskDrag({
    onTaskDrop({ source, taskRecords, isCopy }) {

    }
});
ParameterTypeDescription
sourceGantt
taskRecordsTaskModel[]
isCopyBoolean
onDestroyEvents
onDisableInstancePlugin
onEnableInstancePlugin

Typedefs

2
ParameterTypeDescription
validBoolean

true for valid, false for invalid

messageString

Validation message