Undo/Redo extension implementation with real database and backend

Hey there,

I’ve been using PRO license of Gantt. I want to implement the Undo/Redo functionality using the DHTMLX’s extension for it. However, I am concerned about how this will work with my persistent architecture where every CRUD operation is done through API request and database in sync.

Will turning on the plugin and setting up gantt events, work in my case? How can I use it for tasks creation, update, delete and etc and if possible using the Ctrl + Z for Undo and Ctrl + Y for redo or reverting the changes. These both actions should be send a API request with correct payload data and structure to the backend in order to work accurately.

Please guide me in this, if this extension can be implemented in my case

Hello @Rizwan,

Yes, you can implement undo/redo functionality using the DHTMLX Gantt Undo extension and coordinating the undo/redo actions with your backend API.

First, you need to enable the undo plugin:

gantt.plugins({ undo: true });

It’s possible to configure which actions you want to track. For example:

gantt.config.undo_actions = {
  add: "add",
  update: "update",
  remove: "remove",
  move: "move"
};

You can also specify the entities that the undo operation will be applied for in the gantt.config.undo_types parameter:

gantt.config.undo_types = {
    link:"link",
    task:"task"
};

https://docs.dhtmlx.com/gantt/desktop__undo_redo.html#configuringtheundofunctionality

Also, you can limit how many undo steps are stored (gantt.config.undo_steps).

When you update tasks via code (not through user UI), call gantt.ext.undo.saveState(id, "task") before changing anything, then call gantt.updateTask() after the change. This way, the “before” state is saved and can be undone later.
https://docs.dhtmlx.com/gantt/desktop__undo_redo.html#undoingredoingchangesmadefromcode

It’s also possible to get the stack of the stored undo and redo actions, and clear them if needed: Undo/Redo Functionality Gantt Docs.

Here’s an example of how you can copy, restore, or reset the internal undo/redo stack: DHTMLX Snippet Tool.

Gantt doesn’t automatically bind Ctrl+Z / Ctrl+Y to undo/redo actions, so you need to add this manually:

document.addEventListener("keydown", function(e) {
  if ((e.ctrlKey || e.metaKey) && e.key === "z") {
    gantt.undo();
  }
  if ((e.ctrlKey || e.metaKey) && e.key === "y") {
    gantt.redo();
  }
});

Here’s an example: DHTMLX Snippet Tool.

To keep Gantt state and database in sync and send API requests to the backend, you can listen to events like onAfterUndo and onAfterRedo: Undo/Redo Functionality Gantt Docs.

In the undo/redo handlers, you can iterate over the commands and send the appropriate API requests (create, update, delete) for tasks and links. Based on the command objects (which include type, entity, value, oldValue), send the appropriate API request to your backend.

1 Like

Thank you for quick reply, Valeria. It seems great and understandable. I’ll follow this! and huge thanks for example as well.

1 Like