getTaskType

returns the type of a task

string getTaskType(object task);

Parameters

taskobjectthe task object

Returns

stringthe type of the task

Example

var type = gantt.getTaskType(gantt.getTask(12));

Details
  • If task.type property is defined and not empty, the value of this property will be returned.
  • Otherwise, the value of gantt.config.types.task will be returned instead.

Since the task.type property is optional, you have to take into account that it may be empty when you check the type of the task in code, e.g.

// BAD:
if (task.type === gantt.config.types.task){
    // code specific for task items
}
  • Items that don't have the type property won't meet this condition. This would be incorrect, because such items have the task type by default.

Instead, you can either add a condition for empty values:

// GOOD:
if (!task.type || task.type === gantt.config.types.task){
    // code specific for task items
}

Or use the getTaskType method:

// EVEN BETTER:
if (gantt.getTaskType(task) === gantt.config.types.task){
    // code specific for task items
}

The code will work for all items that have a type specified explicitly, as well as for items that have a default type assigned by the Gantt internal logic.

The following method can be used as a safe way for getting task types, in order to write the same conditions for all types of items, and avoid potential bugs with incorrect type detection:

switch (gantt.getTaskType(task)){
    case gantt.config.task:
        break;
    case gantt.config.project:
        break;
    case gantt.config.milestone:
        break;
}
See also
  • Articles
  • Back to top