Skip to content

Commit 51794af

Browse files
Merge pull request #56 from mlakatkou/GS-3271
[update] multiple-gantts article
2 parents ee0384e + 23e204c commit 51794af

1 file changed

Lines changed: 122 additions & 116 deletions

File tree

docs/guides/multiple-gantts.md

Lines changed: 122 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ sidebar_label: "Multiple Charts on a Page"
99
This functionality is available in the Gantt PRO version under the Commercial (since October 6, 2021), Enterprise and Ultimate licenses
1010
:::
1111

12-
Basically, dhtmlxGantt is a static object and the default instance of it continually exists on the page. You may access it via the global **gantt** object at any time. But you can also create a new gantt object if needed.
12+
Basically, DHTMLX Gantt is a static object and the default instance of it continually exists on the page. You may access it via the global `gantt` object at any time. But you can also create a new gantt object if needed.
1313

1414
## Gantt Instance Configuration
1515

16-
To create a new instance of dhtmlxGantt, use the **Gantt.getGanttInstance()** method:
16+
To create a new instance of DHTMLX Gantt, use the `Gantt.getGanttInstance()` method:
1717

1818
~~~js
1919
// beware, "Gantt" in the command goes with the capital letter
@@ -23,8 +23,8 @@ const ganttChart = Gantt.getGanttInstance();
2323
The method can take a configuration object as a parameter:
2424

2525
~~~js
26-
const gantt = Gantt.getGanttInstance({
27-
plugins:{
26+
const ganttInstance = Gantt.getGanttInstance({
27+
plugins: {
2828
auto_scheduling: true,
2929
},
3030
container: "gantt_here",
@@ -35,27 +35,27 @@ const gantt = Gantt.getGanttInstance({
3535
auto_scheduling: true,
3636
auto_scheduling_strict: true,
3737
auto_scheduling_initial: true,
38-
start_date: new Date(2020, 0, 1),
39-
end_date: new Date(2021, 0, 1),
38+
start_date: new Date(2027, 0, 1),
39+
end_date: new Date(2028, 0, 1),
4040
},
4141
calendars: [
4242
{
43-
id:"global",
43+
id: "global",
4444
worktime: {
4545
hours: ["8:00-17:00"],
46-
days: [ 0, 1, 1, 1, 1, 0 ,0],
46+
days: [0, 1, 1, 1, 1, 0, 0],
4747
customWeeks: {
4848
lastMonthOfYear: {
49-
from: new Date(2020, 11, 1),// December 1st, 2020
50-
to: new Date(2021, 0, 1),// January 1st 00:00, 2021,
49+
from: new Date(2027, 11, 1),
50+
to: new Date(2028, 0, 1),
5151
hours: ["9:00-13:00"],
52-
days: [ 0, 1, 1, 1, 1, 1, 0]
52+
days: [0, 1, 1, 1, 1, 1, 0]
5353
},
54-
firstMonthOfNextYear:{
55-
from: new Date(2021, 0, 1),// January 1st, 2021
56-
to: new Date(2021, 1, 1),// Feb 1st 00:00, 2021,
54+
firstMonthOfNextYear: {
55+
from: new Date(2028, 0, 1),
56+
to: new Date(2028, 1, 1),
5757
hours: ["14:00-16:00"],
58-
days: [ 1, 1, 1, 1, 1, 0, 1]
58+
days: [1, 1, 1, 1, 1, 0, 1]
5959
}
6060
}
6161
}
@@ -64,17 +64,12 @@ const gantt = Gantt.getGanttInstance({
6464
data: {
6565
tasks: [
6666
{ id: 11, text: "Project #1", type: "project", "open": true, "parent": 0 },
67-
{ id: 1, start_date: "05-04-2020", text: "1", duration: 1, parent: "11",
68-
type: "task" },
69-
{ id: 2, start_date: "05-04-2020", text: "2", duration: 3, parent: "11",
70-
type: "task" },
71-
{ id: 3, start_date: "05-04-2020", text: "3", duration: 3, parent: "11",
72-
type: "task" },
73-
{ id: 4, start_date: "05-04-2020", text: "4", duration: 3, parent: "11",
74-
type: "task" },
75-
{ id: 5, start_date: "05-04-2020", text: "5", duration: 1, parent: "11",
76-
type: "task" }
77-
],
67+
{ id: 1, text: "1", start_date: "2027-04-05", duration: 1, parent: "11" },
68+
{ id: 2, text: "2", start_date: "2027-04-05", duration: 3, parent: "11" },
69+
{ id: 3, text: "3", start_date: "2027-04-05", duration: 3, parent: "11" },
70+
{ id: 4, text: "4", start_date: "2027-04-05", duration: 3, parent: "11" },
71+
{ id: 5, text: "5", start_date: "2027-04-05", duration: 1, parent: "11" }
72+
],
7873
links: [
7974
{ source: "1", target: "2", type: "0", id: 1 },
8075
{ source: "1", target: "3", type: "0", id: 2 },
@@ -91,115 +86,122 @@ As a result, you will get an initialized Gantt chart with the specified settings
9186

9287
The config object can contain the following properties:
9388

94-
- **container** - (*string|HTMLElement*) an HTML container (or its id) that the Gantt chart will be displayed in. If not specified, Gantt will be initialized without a container.
95-
- **config** - (*object*) an object with configuration settings of the Gantt chart
96-
- **calendars** - (*array*) an array of worktime calendars to be loaded into the gantt. Calendars must be specified in the format supported by the [gantt.addCalendar](api/method/addcalendar.md) method.
97-
- **templates** - (*object*) an object with templates
98-
- **events** - (*object*) an object with event handlers.
89+
- `container` - (*string|HTMLElement*) an HTML container or its ID where the Gantt chart will be displayed. If not specified, Gantt will be initialized without a container.
90+
- `config` - (*object*) an object with Gantt chart configuration settings
91+
- `calendars` - (*array*) an array of worktime calendars to be loaded into the gantt instance. Calendars must be specified in the format supported by the [`gantt.addCalendar()`](api/method/addcalendar.md) method
92+
- `templates` - (*object*) an object with templates
93+
- `events` - (*object*) an object with event handlers
9994

100-
101-
You need to use the following format while specifying event handlers for a new instance of Gantt:
95+
You need to use the following format while specifying event handlers for a new Gantt instance:
10296

10397
~~~js
104-
const gantt = Gantt.getGanttInstance({
105-
events: {
106-
onTaskCreated: function(task){
107-
task.owner = null;
108-
return true;
109-
},
110-
onTaskClick: function(id){
111-
alert(gantt.getTask(id).text);
112-
return true;
113-
}
114-
}
115-
})
98+
const ganttInstance = Gantt.getGanttInstance({
99+
events: {
100+
onTaskCreated: (task) => {
101+
task.owner = null;
102+
return true;
103+
},
104+
onTaskClick: (taskId) => {
105+
alert(ganttInstance.getTask(taskId).text);
106+
return true;
107+
}
108+
}
109+
});
116110
~~~
117111

118-
- **data** - (*object|string*) an object with data to load or the URL to load data from
119-
- **plugins** - (*object*) extensions that need to be activated
120-
- **locale** - (*string|object*) a two-letter language code or an object of the locale that needs to be activated
112+
- `data` - (*object|string*) an object with data to load or the URL to load data from
113+
- `plugins` - (*object*) extensions that need to be activated
114+
- `locale` - (*string|object*) a two-letter language code or an object of the locale that needs to be activated
121115

122-
**Note**, that calling the **Gantt.getGanttInstance()** method without parameters will return the gantt object with default configuration settings.
123-
Therefore, you will need to configure your new instance, initialize it and populate with data, as usual.
116+
Note that calling the `Gantt.getGanttInstance()` method without parameters will return a gantt object with the default configuration settings.
117+
Therefore, you will need to configure your new instance, initialize it, and populate it with data, as usual.
124118

125-
Let's take a simple example: 2 Gantt charts, one under another:
119+
Let's take a simple example: two Gantt charts, one under another:
126120

127121
~~~js
128-
window.addEventListener("DOMContentLoaded", function(){
129-
var gantt1 = Gantt.getGanttInstance();
130-
gantt1.init("gantt_here");
131-
gantt1.parse(tasksA);
132-
133-
var gantt2 = Gantt.getGanttInstance();
134-
gantt2.init("gantt_here_2");
135-
gantt2.parse(tasksB);
122+
window.addEventListener("DOMContentLoaded", () => {
123+
const firstGantt = Gantt.getGanttInstance();
124+
firstGantt.init("gantt_here");
125+
firstGantt.parse(tasksA);
126+
127+
const secondGantt = Gantt.getGanttInstance();
128+
secondGantt.init("gantt_here_2");
129+
secondGantt.parse(tasksB);
136130
});
131+
~~~
137132

133+
~~~html
138134
<body>
139-
<div id="gantt_here" style="width:100%; height: 50%;"></div>
140-
<div id="gantt_here_2" style="width:100%; height: 50%;"></div>
135+
<div id="gantt_here" style="width: 100%; height: 50%;"></div>
136+
<div id="gantt_here_2" style="width: 100%; height: 50%;"></div>
141137
</body>
142138
~~~
143139

144-
## Integration with dhtmlxLayout
140+
## Integration with DHTMLX Layout
145141

146-
A good way to place multiple Gantt charts on the page is using [dhtmlxLayout](https://docs.dhtmlx.com/layout__index.html).
147-
It not only provides a beautiful frame, but also ensures correct interacting with other elements on the page and acting according to the page size changes.
142+
A good way to place multiple Gantt charts on the page is using [DHTMLX Layout](https://docs.dhtmlx.com/suite/layout/).
143+
It not only provides a convenient layout frame, but also ensures correct interaction with other elements on the page and responds to page size changes.
148144

149145
:::note
150-
Note that dhtmlxLayout is a separate product, not a part of the dhtmlxGantt library. If you would like to use dhtmlxLayout in your application, you should purchase the license.
151-
Please [check the licensing options](https://dhtmlx.com/docs/products/dhtmlxLayout/#editions-licenses).
146+
Note that DHTMLX Layout is a separate product, not a part of the DHTMLX Gantt library. If you would like to use DHTMLX Layout in your application, you should purchase the license.
147+
Please [check the licensing options](https://dhtmlx.com/docs/products/licenses.shtml).
152148
:::
153149

154-
**To attach a dhtmlxGantt instance to a layout cell**, use the **attachGantt()** method.
155-
156-
**Note**, attaching dhtmlxGantt to a cell automatically initializes it. So, configure dhtmlxGantt instance before placing it into the layout.
150+
A DHTMLX Gantt instance can be placed into a layout cell by defining a container in the cell and initializing Gantt in it.
157151

158152
~~~js
159-
function init() {
160-
var dhxLayout = new dhtmlXLayoutObject(document.body, "2U");
161-
162-
gantt1 = Gantt.getGanttInstance();
163-
gantt1.config.min_column_width = 50;
164-
gantt1.config.scale_height = 90;
165-
dhxLayout.cells("a").attachGantt(null, null, gantt); /*!*/
166-
gantt1.parse(tasksA);
167-
168-
gantt2 = Gantt.getGanttInstance();
169-
gantt2.config.date_grid = "%Y-%m-%d %H:%i";
170-
gantt2.config.xml_date = "%Y-%m-%d %H:%i:%s";
171-
dhxLayout.cells("b").attachGantt(null, null, gantt2);/*!*/
172-
gantt2.parse(tasksB);
173-
}
153+
new dhx.Layout("layout_container", {
154+
rows: [
155+
{
156+
id: "top",
157+
height: "50%",
158+
html: '<div id="gantt_here" style="width: 100%; height: 100%;"></div>'
159+
},
160+
{
161+
id: "bottom",
162+
height: "50%",
163+
html: '<div id="gantt_here_2" style="width: 100%; height: 100%;"></div>'
164+
}
165+
]
166+
});
167+
168+
const firstGantt = Gantt.getGanttInstance();
169+
firstGantt.init("gantt_here");
170+
firstGantt.parse(tasksA);
171+
172+
const secondGantt = Gantt.getGanttInstance();
173+
secondGantt.init("gantt_here_2");
174+
secondGantt.parse(tasksB);
174175
~~~
175176

176177
## Destructor of Gantt and DataProcessor instances {#destructorofganttanddataprocessorinstances}
177178

178-
Starting from version 5.1, the dhtmlxGantt object has a [destructor](api/method/destructor.md) that can be used to dispose unnecessary instances of the Gantt.
179+
Starting from version 5.1, the DHTMLX Gantt object has a [`destructor()`](api/method/destructor.md) method that can be used to dispose unnecessary Gantt instances.
179180

180-
The destructor of the gantt instance can be used as follows:
181+
The `destructor()` method of a gantt instance can be used as follows:
181182

182183
~~~js
183-
var gantt = Gantt.getGanttInstance();
184-
gantt.destructor();
184+
const ganttInstance = Gantt.getGanttInstance();
185+
ganttInstance.destructor();
185186
~~~
186187

187-
The destructor will implement the following tasks:
188+
The destructor will perform the following tasks:
188189

189190
- clear the data loaded into a gantt instance
190-
- destroy the dataProcessor (if it is attached to the gantt)
191+
- destroy the data processor if it is attached to the gantt
191192
- detach the gantt from DOM
192-
- detach all DOM events attached via the [gantt.event()](api/method/event.md) method
193+
- detach all DOM events attached via the [`gantt.event()`](api/method/event.md) method
193194

194-
Note, that the destructor won't destroy the data stores created by the [gantt.createDatastore()](api/method/createdatastore.md) method.
195+
Note that the destructor will not destroy the data stores created by the [`gantt.createDatastore()`](api/method/createdatastore.md) method.
195196
You have to destroy them manually, like this:
196197

197198
~~~js
198199
// creating a datastore
199-
var resourcesStore = gantt.createDatastore({
200-
name:"resource",
201-
initItem: function(item){
202-
item.id = item.key || gantt.uid();
200+
const ganttInstance = Gantt.getGanttInstance();
201+
const resourcesStore = ganttInstance.createDatastore({
202+
name: "resource",
203+
initItem: (item) => {
204+
item.id = item.key || ganttInstance.uid();
203205
return item;
204206
}
205207
});
@@ -212,36 +214,40 @@ resourcesStore.destructor();
212214

213215
Here is an example of using the destructor to dispose a gantt instance while using the Angular framework:
214216

215-
~~~js
216-
@Component({selector: 'app-gantt', template: `...`})
217-
class MyGanttComponent implements OnDestroy {
218-
ngOnInit() {
219-
this.$gantt = Gantt.getGanttInstance();
220-
221-
// configure and init
222-
}
223-
224-
ngOnDestroy() {
225-
this.$gantt.destructor();
226-
this.$gantt = null;
227-
}
217+
~~~ts
218+
@Component({ template: '...' })
219+
class MyGanttComponent implements OnInit, OnDestroy {
220+
private ganttInstance;
221+
222+
ngOnInit() {
223+
this.ganttInstance = Gantt.getGanttInstance();
224+
225+
// configure and init
226+
}
227+
228+
ngOnDestroy() {
229+
if (this.ganttInstance) {
230+
this.ganttInstance.destructor();
231+
}
232+
}
228233
}
229234
~~~
230235

231236
### Detaching the dataProcessor
232237

233-
Calling the destructor of data processor will clear the dataprocessor instance and detach it from the gantt. For example:
238+
Calling the destructor of a data processor will clear the data processor instance and detach it from the gantt. For example:
234239

235240
~~~js
236-
var gantt = Gantt.getGanttInstance();
237-
var dp = new gantt.dataProcessor("url");
238-
dp.init(gantt);
241+
const ganttInstance = Gantt.getGanttInstance();
242+
const dataProcessor = ganttInstance.createDataProcessor({
243+
url: "url",
244+
mode: "REST"
245+
});
239246

240247
// destroys data processor and detaches it from the gantt
241-
dp.destructor();
248+
dataProcessor.destructor();
242249
~~~
243250

244251
:::note
245-
If you use a package that does not allow creating multiple instances of the gantt object (GPL or Commercial editions), calling the gantt destructor will make the gantt inaccessible until page reload.
252+
If you use a package that does not allow creating multiple instances of the gantt object (GPL or Commercial editions), calling the gantt destructor will make the gantt inaccessible until the page is reloaded.
246253
:::
247-

0 commit comments

Comments
 (0)