How to Collapse all tasks and project at the beginning


I want to collapse all the tasks and collapse the project at the beginning,but I don’t know how.


For the moment there is no method to do that directly. We will add it in the future version.
You might want to take a look at “GanttTask.prototype.createTreeImg” implementation and add your own code like that.


I hope we are in the future now since I am also looking for the possibility to collapse all tasks on load. :smiley:

Has this function been implemented?


You can control the initial state by using open_tree_initially config option, … onfig.html

Also, there is open API
which can be combined with eachTask iterator to change state of all items in the gantt

And last, you can define open property in an xml|json data. It allows to configure initial open state for each branch separately. … properties


Here are functions to do the trick.
I’ve included an “if” test to only work on projects but you can remove it to work on all levels.

   function closeAll()
            if (task2close.$level == 0) { //is a project, not a task
    function openAll()
            if (task2open.$level == 0) { //is a project, not a task



if you want to collapse tasks initially, I’d suggest setting the open state in onTaskLoading event:

gantt.attachEvent("onTaskLoading", function(task){
    task.$open = false;
    return true;


As for opening and closing project tree on demand, you can do the following:

function closeAll(){
		task.$open = false;
function openAll(){
		task.$open = true;

It does pretty much the same thing your code does, the difference is that each call of gantt.close or can fire a complete repaint of the gantt. Potentially, it can lead to redundant repaints if you have multiple tasks at the top level