Agent-readable demo data

Title
External Drag and Drop (JavaScript Gantt Chart) | DayPilot Pro for JavaScript Demo
Tree
DayPilot JavaScript Demo
Catalog root
Default
Description
You can drag tasks from an external list to the Gantt chart.

Inline demo script 1

const dp = new DayPilot.Gantt("dp", {
            startDate: DayPilot.Date.today().addDays(-1),
            days: 60,
            columns: [
                {title: "Name", width: 50, property: "text"},
                {title: "Info", width: 50, property: "info"},
                {title: "Duration", width: 50}
            ],
            onBeforeRowHeaderRender: args => {
                const duration = args.task.duration();
                const html = duration.toString("d") + "d " + duration.toString("h") + "h";
                args.row.columns[2].html = html;
            },
            contextMenuTask: new DayPilot.Menu({
                items: [
                    {
                        text: "Show task ID", onClick: args => {
                            DayPilot.Modal.alert("Task ID: " + args.source.id());
                        }
                    },
                    {
                        text: "Delete task", onClick: args => {
                            dp.tasks.remove(args.source);
                        }
                    }
                ]
            }),
            contextMenuRow: new DayPilot.Menu({
                items: [
                    {
                        text: "Show task ID", onClick: args => {
                            DayPilot.Modal.alert("Task ID: " + args.source.id());
                        }
                    },
                    {
                        text: "Delete task", onClick: args => {
                            dp.tasks.remove(args.source);
                        }
                    }
                ]
            }),
        });
        dp.init();

        const app = {
            elements: {
                external: document.getElementById("external"),
                queue: document.getElementById("queue")
            },
            init() {
                this.makeDraggable();
                this.registerDropTarget();
                this.loadData();
            },
            loadData() {
                const tasks = [];
                const links = [];

                let start = DayPilot.Date.today();
                let last = null;
                for (let i = 0; i < 5; i++) {
                    const duration = Math.floor(Math.random() * 3) + 1; // 1 to 3
                    const end = start.addDays(duration);

                    const task = {
                        start: start,
                        end: end,
                        id: DayPilot.guid(),
                        text: "Task " + i,
                        tags: {
                            info: "info text"
                        },
                        children: [
                            {start: start, end: end, id: DayPilot.guid(), text: "Subtask 1", complete: Math.floor(Math.random() * 101)},
                            {start: start, end: end, id: DayPilot.guid(), text: "Subtask 2", complete: Math.floor(Math.random() * 101)},
                            {start: end, id: DayPilot.guid(), text: "Milestone 1", type: "Milestone"}

                        ]
                    };

                    tasks.push(task);

                    if (last) {
                        links.push({
                            id: DayPilot.guid(),  // optional
                            from: last,
                            to: task.id,
                            type: "FinishToStart"
                        });
                    }
                    last = task.id;
                    start = end;

                }
                dp.update({tasks, links});
            },
            makeDraggable() {
                for (const element of this.elements.external.querySelectorAll("li")) {
                    this.makeItemDraggable(element);
                }
            },
            makeItemDraggable(element) {
                DayPilot.Gantt.makeDraggable({
                    element,
                    remove: element,
                    data: {
                        id: element.getAttribute("data-id"),
                        text: element.innerText,
                        duration: DayPilot.Duration.hours(element.getAttribute("data-duration")),
                        box: {
                            barColor: "#a12424",
                            barBackColor: "#b88888"
                        }
                    },
                    onDragStart: args => {
                        args.options.element.style.opacity = "0.5";
                    },
                    onDrop: args => {
                        args.options.element.style.opacity = "";
                    }
                });
            },
            registerDropTarget() {
                DayPilot.Gantt.registerDropTarget({
                    element: this.elements.queue,
                    onDrop: args => this.returnTask(args.task),
                    onDragOver: () => {
                        this.elements.queue.style.backgroundColor = "#f8f8f8";
                    },
                    onDragLeave: () => {
                        this.elements.queue.style.backgroundColor = "";
                    }
                });
            },
            returnTask(task) {
                const element = document.createElement("li");
                element.setAttribute("data-duration", task.duration().totalHours());
                element.setAttribute("data-id", task.id());
                element.style.cursor = "move";
                element.innerText = task.text();
                this.elements.external.appendChild(element);
                this.makeItemDraggable(element);
                dp.tasks.remove(task);
                this.elements.queue.style.backgroundColor = "";
            }
        };
        app.init();