Bug when calling gantt.parse more than once?

I have a gantt that can load different projects and I have noticed that calling gantt.parse more than once does not replace the existing tasks etc. but adds tasks to the current gantt definition, so I end up with an amalgamation of the task definitions (see tasks from both projects), so my questions are:

  1. Is this a bug? I would expected gantt.parse to replace all info with the new json definition

  2. Using gantt.clear() before gantt.parse seems to solve the issue but there are some problems with the order of calling

a) This give an error (“gantt_core.js:175 Uncaught TypeError: this._getHorizontalScrollbar is not a functio…”)
and I also lose all my gantt config:

.... gantt config for resources and zoom etc. goes here ....
gantt.clearAll();
gantt.init("gantt_here");    

b) This gives no error and solves the original problem but obviously I still lose all my gantt config:

.... gantt config for resources and zoom etc. goes here ....
gantt.init("gantt_here");   
gantt.clearAll();

c) This gives no error and solves the original problem and keeps the gantt config BUT all the demos I have seen call gantt.init AFTER all the config is setp:

gantt.init("gantt_here");   
gantt.clearAll();
.... gantt config for resources and zoom etc. goes here ....

So if the behaviour is not buggy then is it correct and safe to do as per 2© above?

Hello,
If task IDs are the same, Gantt should replace those tasks. Here is an example:
http://snippet.dhtmlx.com/5/27cce27b0

If there are different IDs then yes, Gantt doesn’t remove existing tasks.

The gantt.clearAll() method shouldn’t remove the Gantt configuration and attached events, It should only remove the loaded tasks.
The configuration can be reset if you destroy the Gantt instance and create a new one.

Usually, Gant configuration is applied before the Gantt initialization. You can put if after initializing Gantt, but in some cases, you will need to call the gantt.render() or gantt.init() methods to apply the changes.

If it doesn’t work in your case, it is hard to suggest what might be wrong as I don’t see your code. Please send me a ready demo that I can run so that I can reproduce the issue locally and check what might be wrong.

Perhaps I was mistaken when I said that gantt.clearAll() removes events etc. as I can’t replicate this BUT it does appear to remove the datastore. The best option I have is to use:

.... gantt config for resources/zoom/resources etc. goes here ....
gantt.init("gantt_here");   
gantt.clearAll();
gantt.parse(json);

Then my gantt parses different json defs as expected but there is an issue when using the datastore:

gantt.config.columns = [
    ... other column defs here....
    {name: "owner",     label: "Owner",align: "center", width: 75,  resize:true, template: function (task) {
                                
      var store = gantt.getDatastore("resource_store");
      store.getItems() //This is empty when clearAll is used but is populated as expected when clearAll is not used
        ... some code here to return owner field template ...
    }
];

So it seems that clearAll() actually does have an affect on the config (at least in terms of the datastore anyway), here is a working snippit (just toggle gantt.clearAll()) to see the behaviour of the owner column):

http://snippet.dhtmlx.com/5/7dedbd65d

Hello,
Thank you for the clarification. Yes, it is true, that the gantt.clearAll() method clears all data stores. But I don’t think we can call it Gantt configuration, as it only stores the data of tasks, links, resources, and other data collections.
If you want to clear only the task data store, you need to use the following command:

gantt.getDatastore("task").clearAll();

https://docs.dhtmlx.com/gantt/api__gantt_getdatastore.html
Here is the updated snippet:
http://snippet.dhtmlx.com/5/90c042e4a

ok thanks, so this is what I should call before parsing a new gantt json definition? ie. it will keep the config and resource datastore but remove all links and everything related to the ‘old’ gantt definition

gantt.getDatastore("task").clearAll();

Hello,
The gantt.getDatastore("task") command will return the task Datastore. If you want to remove links, you need to call the following commands:

gantt.getDatastore("task").clearAll();
gantt.getDatastore("link").clearAll();

With these commands only tasks and links will be removed. Other things should remain.