Skip to content

Commit 662b533

Browse files
Merge pull request #6 from Valeria110/GS-3035
[dev] update Gantt + Node.js tutorial
2 parents 98c0b93 + 101fdb8 commit 662b533

4 files changed

Lines changed: 28 additions & 25 deletions

File tree

docs/integrations/node/howtostart-nodejs.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ When the file is ready, open it and put the above listed dependencies into it. T
6060
~~~js title="package.json"
6161
{
6262
"name": "dhx-gantt-app",
63-
"version": "1.0.2",
63+
"version": "1.0.3",
6464
"description": "",
6565
"main": "server.js",
6666
"dependencies": {
67-
"body-parser": "^1.19.1",
68-
"express": "^4.17.2"
67+
"body-parser": "^2.2.1",
68+
"express": "^5.2.1"
6969
},
7070
"scripts": {
7171
"test": "echo "Error: no test specified" && exit 1",
@@ -198,21 +198,21 @@ CREATE TABLE `gantt_tasks` (
198198

199199
and add some test data:
200200
~~~js
201-
INSERT INTO `gantt_tasks` VALUES ('1', 'Project #1', '2017-04-01 00:00:00',
201+
INSERT INTO `gantt_tasks` VALUES ('1', 'Project #1', '2026-04-01 00:00:00',
202202
'5', '0.8', '0');
203-
INSERT INTO `gantt_tasks` VALUES ('2', 'Task #1', '2017-04-06 00:00:00',
203+
INSERT INTO `gantt_tasks` VALUES ('2', 'Task #1', '2026-04-06 00:00:00',
204204
'4', '0.5', '1');
205-
INSERT INTO `gantt_tasks` VALUES ('3', 'Task #2', '2017-04-05 00:00:00',
205+
INSERT INTO `gantt_tasks` VALUES ('3', 'Task #2', '2026-04-05 00:00:00',
206206
'6', '0.7', '1');
207-
INSERT INTO `gantt_tasks` VALUES ('4', 'Task #3', '2017-04-07 00:00:00',
207+
INSERT INTO `gantt_tasks` VALUES ('4', 'Task #3', '2026-04-07 00:00:00',
208208
'2', '0', '1');
209-
INSERT INTO `gantt_tasks` VALUES ('5', 'Task #1.1', '2017-04-05 00:00:00',
209+
INSERT INTO `gantt_tasks` VALUES ('5', 'Task #1.1', '2026-04-05 00:00:00',
210210
'5', '0.34', '2');
211-
INSERT INTO `gantt_tasks` VALUES ('6', 'Task #1.2', '2017-04-11 13:22:17',
211+
INSERT INTO `gantt_tasks` VALUES ('6', 'Task #1.2', '2026-04-11 13:22:17',
212212
'4', '0.5', '2');
213-
INSERT INTO `gantt_tasks` VALUES ('7', 'Task #2.1', '2017-04-07 00:00:00',
213+
INSERT INTO `gantt_tasks` VALUES ('7', 'Task #2.1', '2026-04-07 00:00:00',
214214
'5', '0.2', '3');
215-
INSERT INTO `gantt_tasks` VALUES ('8', 'Task #2.2', '2017-04-06 00:00:00',
215+
INSERT INTO `gantt_tasks` VALUES ('8', 'Task #2.2', '2026-04-06 00:00:00',
216216
'4', '0.9', '3');
217217
~~~
218218

@@ -286,12 +286,11 @@ async function serverСonfig() {
286286

287287
for (let i = 0; i < tasks.length; i++) {
288288
tasks[i].start_date = tasks[i].start_date.format("YYYY-MM-DD hh:mm:ss");
289-
tasks[i].open = true;
290289
}
291290

292291
res.send({
293292
data: tasks,
294-
collections: { links: links }
293+
collections: { links }
295294
});
296295

297296
}).catch(error => {
@@ -321,19 +320,18 @@ What we have done in this code:
321320
- opened MySql connection to our database
322321
- defined that on the <b>GET /data</b> request we'll read data from tasks and links tables and format them so they could be parsed on the client
323322

324-
Note that we've also added the *open* property to ensure that the tasks tree will be initially expanded.
325-
326323
Now, we can call this route from the client:
327324

328325
~~~js title="public/index.html"
329326
gantt.config.date_format = "%Y-%m-%d %H:%i:%s";/*!*/
327+
gantt.config.open_tree_initially = true;
330328

331329
gantt.init("gantt_here");
332330

333331
gantt.load("/data");/*!*/
334332
~~~
335333

336-
Note that [date_format](api/config/date_format.md) config specifies the format of dates (<b>start_date</b> of the task) that comes from the server.
334+
Note that [date_format](api/config/date_format.md) config specifies the format of dates (<b>start_date</b> of the task) that comes from the server. The [gantt.config.open_tree_initially](api/config/open_tree_initially.md) config is set to `true` to ensure that the tasks tree will be initially expanded.
337335

338336
Let's run the application now by opening http://127.0.0.1:1337. The gantt will be loaded with the test data that we have previously added into the database.
339337

@@ -344,7 +342,7 @@ Step 5. Saving changes
344342

345343
The last thing that we should implement is data saving.
346344
For this we need a code that will send updates happening on the client side back to the server.
347-
Go to *public/index.html* and add [gantt.dataProcessor](guides/server-side.md#technique) to the page:
345+
Go to *public/index.html* and add [gantt.createDataProcessor](guides/server-side.md#technique) to the page:
348346

349347

350348
~~~js title="public/index.html"
@@ -354,9 +352,10 @@ gantt.init("gantt_here");
354352

355353
gantt.load("/data");
356354

357-
const dp = new gantt.dataProcessor("/data");/*!*/
358-
dp.init(gantt);/*!*/
359-
dp.setTransactionMode("REST");/*!*/
355+
const dp = gantt.createDataProcessor({ /*!*/
356+
url: '/data', /*!*/
357+
mode: 'REST', /*!*/
358+
}); /*!*/
360359
~~~
361360

362361
Let's go deeper and see what role it plays.
@@ -377,10 +376,11 @@ The resulting code will be rather spacious:
377376
// add a new task
378377
app.post("/data/task", (req, res) => {
379378
let task = getTask(req.body);
379+
const { text, start_date, duration, progress, parent } = task;
380380

381381
db.query("INSERT INTO gantt_tasks(text, start_date, duration, progress, parent)"
382382
+ " VALUES (?,?,?,?,?)",
383-
[task.text, task.start_date, task.duration, task.progress, task.parent])
383+
[text, start_date, duration, progress, parent])
384384
.then(result => {
385385
sendResponse(res, "inserted", result.insertId);
386386
})
@@ -393,10 +393,11 @@ app.post("/data/task", (req, res) => {
393393
app.put("/data/task/:id", (req, res) => {
394394
let sid = req.params.id,
395395
task = getTask(req.body);
396+
const { text, start_date, duration, progress, parent } = task;
396397

397398
db.query("UPDATE gantt_tasks SET text = ?, start_date = ?, "
398399
+ "duration = ?, progress = ?, parent = ? WHERE id = ?",
399-
[task.text, task.start_date, task.duration, task.progress, task.parent, sid])
400+
[text, start_date, duration, progress, parent, sid])
400401
.then(result => {
401402
sendResponse(res, "updated");
402403
})
@@ -421,9 +422,10 @@ app.delete("/data/task/:id", (req, res) => {
421422
// add a link
422423
app.post("/data/link", (req, res) => {
423424
let link = getLink(req.body);
425+
const { source, target, type } = link;
424426

425427
db.query("INSERT INTO gantt_links(source, target, type) VALUES (?,?,?)",
426-
[link.source, link.target, link.type])
428+
[source, target, type])
427429
.then(result => {
428430
sendResponse(res, "inserted", result.insertId);
429431
})
@@ -436,9 +438,10 @@ app.post("/data/link", (req, res) => {
436438
app.put("/data/link/:id", (req, res) => {
437439
let sid = req.params.id,
438440
link = getLink(req.body);
441+
const { source, target, type, sid } = link;
439442

440443
db.query("UPDATE gantt_links SET source = ?, target = ?, type = ? WHERE id = ?",
441-
[link.source, link.target, link.type, sid])
444+
[source, target, type, sid])
442445
.then(result => {
443446
sendResponse(res, "updated");
444447
})
@@ -559,7 +562,7 @@ app.get("/data", (req, res) => {
559562

560563
res.send({
561564
data: tasks,
562-
collections: { links: links }
565+
collections: { links }
563566
});
564567

565568
}).catch(error => {

static/img/gantt_init.png

-878 Bytes
Loading

static/img/load_data_nodejs.png

7.24 KB
Loading

static/img/ready_gantt_nodejs.png

-946 Bytes
Loading

0 commit comments

Comments
 (0)