Work with History module
This functionality requires PRO version of the DHTMLX Grid (or DHTMLX Suite) package.
You can manage the history of actions in the Grid component via the API of the History
module. It allows tracking changes made by the user through the user interface, such as editing cells using the editor, clearing cells, or applying modifications via the handle in the BlockSelection
mode. The module supports the undo
and redo
operations, as well as allows managing the history stack with the ability to limit its size.
Initializing the History module
To initialize the History
module, use the history
property in the configuration of the dhx.Grid component. When the Grid is created, the module is accessible through the grid.history
property.
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "a", header: [{ text: "A" }] },
{ id: "b", header: [{ text: "B" }] },
],
data: [
{ id: "1", a: "A1", b: "B1" },
{ id: "2", a: "A2", b: "B2" },
],
history: true // enables the History module
});
The history
property can also be set as an object to enable the module and provide additional configuration options.
Learn about configuration possibilities of the History
module in the Configuration guide.
Related sample: Grid. History. Configuration
Enabling/disabling History module
You can activate the history module via the enable()
method of the history
object. The following example shows how the module is enabled after deactivation on initialization:
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "name", header: [{ text: "Name" }] },
{ id: "value", header: [{ text: "Value" }] },
],
data: [
{ id: "1", name: "Item 1", value: 10 },
],
history: { disabled: true }
});
// checking the state
console.log(grid.history.isDisabled()); // -> true
// enabling the module
grid.history.enable();
console.log(grid.history.isDisabled()); // -> false
// adding an action
grid.history.add({
type: "change",
batch: [{ id: "1", name: "Updated Item", value: 15 }],
inverse: { type: "change", batch: [{ id: "1", name: "Item 1", value: 10 }] },
});
console.log(grid.history.getHistory().length); // -> 1
To disable the management of the history of actions in Grid, use the disable()
method of the history
object. The following example shows disabling of the module after adding an action:
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "name", header: [{ text: "Name" }] },
{ id: "value", header: [{ text: "Value" }] },
],
data: [
{ id: "1", name: "Item 1", value: 10 },
],
history: true
});
// adding an action
grid.history.add({
type: "change",
batch: [{ id: "1", name: "Updated Item", value: 15 }],
inverse: { type: "change", batch: [{ id: "1", name: "Item 1", value: 10 }] },
});
// disabling the module
grid.history.disable();
console.log(grid.history.isDisabled()); // -> true
// attempting to add a new action
grid.history.add({
type: "add",
batch: [{ id: "2", name: "New Item", value: 20 }],
});
console.log(grid.history.getHistory().length); // -> 1 (the new action hasn't been added)
Checking History module state
You can check whether the History
module is disabled, using the isDisabled()
method of the history
object. It returns true
, if the module is disabled and false
, if it is enabled. The following example shows checking the module's state after disabling it:
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "name", header: [{ text: "Name" }] },
{ id: "value", header: [{ text: "Value" }] },
],
data: [
{ id: "1", name: "Item 1", value: 10 },
],
history: true
});
grid.history.disable();
console.log(grid.history.isDisabled()); // -> true
grid.history.enable();
console.log(grid.history.isDisabled()); // -> false
Adding/removing Grid history actions
Types of actions
The History
module supports the following action types within the Grid component:
- "add" - adding rows to the table
- "remove" - removing rows from the table
- "removeAll" - clearing all the rows
- "change" - modifying cell values (requires specifying an inverse action)
For the "change" and "removeAll" actions, an inverse
action is mandatory. Otherwise, adding the action to the history will trigger an error.
Adding a new action
You can add a new action into the history of actions within the grid by using the add()
method of the history
object. The method takes the following parameters:
action | (object) the action object that contains the following properties:
|
The action
argument must conform to the IAction
interface. If the module is disabled, the action type is invalid, or the inverse
property is missing (for actions with the "change" and "removeAll" types), the error
event is triggered.
All the actions performed through the API (e.g., using grid.data
) can be added to the history using the add()
method. The following example shows adding a new row via DataCollection
:
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "name", header: [{ text: "Name" }] },
{ id: "value", header: [{ text: "Value" }] },
],
data: [
{ id: "1", name: "Item 1", value: 10 },
],
history: true
});
// adding a new row via DataCollection
const newItem = { id: "2", name: "New Item", value: 20 };
grid.data.add(newItem);
// recording the action in the history
grid.history.add({
type: "add",
batch: [{ ...newItem }],
});
// checking the history
const history = grid.history.getHistory();
console.log(history.length); // ->1
Related sample: Grid. History. Adding a custom action
To make the process of adding new actions into the Grid history more flexible, you can apply the related events of the history
object:
Removing the last action
You can remove the last action from the Grid history using the remove()
method of the history
object. Note that if the history is empty or the module is disabled, the operation is ignored and the error
event is triggered (for a disabled module).
The following example shows removing the last action from the history after modifying a value:
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "name", header: [{ text: "Name" }] },
{ id: "value", header: [{ text: "Value" }] },
],
data: [
{ id: "1", name: "Item 1", value: 10 },
],
history: true
});
// modifying a value via DataCollection
const updatedItem = { id: "1", name: "Updated Item", value: 15 };
grid.data.update("1", updatedItem);
// adding the action to history
grid.history.add({
type: "change",
batch: [{ ...updatedItem }],
inverse: { type: "change", batch: [{ id: "1", name: "Item 1", value: 10 }] },
});
// removing the last action
grid.history.remove();
console.log(grid.history.getHistory().length); // -> 0