addTask

adds a new task

string| number addTask(NewTask task, [string | number parent,number index] );
taskNewTaskthe task object
parentstring | numberoptional, the parent's id
indexnumberoptional, the position the task will be added into (0 or greater)
string| numberthe task's id

Example

var taskId = gantt.addTask({
    id:10,
    text:"Task #5",
    start_date:"02-09-2013",
    duration:28
}, "project_2", 1);

Details

If you set the index parameter with the value from 0 and greater, a task will be added to the specified position in the branch. Otherwise, the task will be added to the end of the tasks' branch.

The method invokes the onBeforeTaskAdd and onAfterTaskAdd events.

Note, if you don't want to save task in case, for example, the user clicks the "Cancel" button in the lightbox, use the createTask method and the onTaskCreated event that this method invokes.

Preventing from adding tasks to certain levels

A quite easy way to prevent users from adding sub-tasks to specific tasks is to hide the 'Add' button through CSS.

  1. First, assign a CSS class for each task row using the grid_row_class template:
    gantt.templates.grid_row_class = function( start, end, task ){
        if ( task.$level > 1 ){
            return "nested_task"
        }
        return "";
    };
  2. Then, hide the 'Add' button for such rows:
    .nested_task .gantt_add{
        display: none !important;
    }

Related sample:  Predefined Project Structure

See also
Back to top