State Persistence Feature

21 Dec 20227 minutes to read

State persistence refers to the Gantt’s state maintained in the browser’s localStorage even if the browser is refreshed or if you move to the next page within the browser.
State persistence stores gantt’s model object in the local storage when the enablePersistence is defined as true.

Get or set localStorage value

If the enablePersistence property is set to true, the gantt property value is saved in the window.localStorage for reference. You can get/set the localStorage value by using the getItem/setItem method in the window.localStorage.

//get the Gantt model.
var value = window.localStorage.getItem('ganttGantt'); //"ganttGantt" is component name + component id.
var model = JSON.parse(model);
//set the Gantt model.
window.localStorage.setItem('ganttGantt', JSON.stringify(model)); //"ganttGantt" is component name + component id.

Prevent columns from persisting

When the EnablePersistence property is set to true, the Gantt properties such as Filtering, Sorting, and Columns will persist. You can use the addOnPersist method to prevent these Gantt properties from persisting.

The following example demonstrates how to prevent Gantt columns from persisting. In the DataBound event of the Gantt, you can override the addOnPersist method and remove the columns from the key list given for persistence.

NOTE

When the EnablePersistence property is set to true, the Gantt properties such as column template, column formatter, header text, and value accessor will not persist.

<ejs-button id="add" content="Add Columns" isPrimary="true"></ejs-button>
<ejs-button id="remove" content="Remove Columns" isPrimary="true"></ejs-button>
<ejs-gantt id="Gantt" dataSource="@ViewBag.Datasource" dataBound="onBound" enablePersistence="true" height="270"> 
    <e-gantt-columns>
        <e-gantt-column field="TaskID" headerText="Task ID" type="number" textAlign="Right" width="120"></e-gantt-column>
        <e-gantt-column field="Task Name" headerText="Task ID"  width="150"></e-gantt-column>
        <e-gantt-column field="Start Date" headerText="Start Date"  width="150"></e-gantt-column>
        <e-gantt-column field="Duration" headerText="Duration"  width="150"></e-gantt-column>
    </e-gantt-columns>
</ejs-gantt>

<script>
function onBound(args) {
    var cloned = this.addOnPersist;
    this.addOnPersist = function (key) {
        key = key.filter(item => item !== "columns");
        return cloned.call(this, key);
    };
} 

document.getElementById('add').onclick = function () {
    var obj = { field: "Progress", headerText: 'Progress', width: 100 };
    document.getElementById('Gantt').ej2_instances[0].columns.push(obj); //you can add the columns by using the Gantt columns method
    document.getElementById('Gantt').ej2_instances[0].treeGrid.refreshColumns();
}

document.getElementById('remove').onclick = function () {
    document.getElementById('Gantt').ej2_instances[0].columns.pop();
    document.getElementById('Gantt').ej2_instances[0].treeGrid.refreshColumns();
}
</script>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.EditingData();	
    return View();
}

Persist the header template and header Text

By default, the Gantt properties such as column template, header text, header template, column formatter, and value accessor will not persist when EnablePersistence is set to true. Because the column template and header text can be customized at the application level.

If you wish to restore all these column properties, then you can achieve it by cloning the gantt’s columns property using JavaScript Object’s assign method and storing this along with the persist data manually. While restoring the settings, this column object must be assigned to the gantt’s columns property to restore the column settings as demonstrated in the following sample.

<ejs-button id="restore" content="Restore" isPrimary="true"></ejs-button>
<ejs-gantt id="Gantt" dataSource="@ViewBag.datasource" enablePersistence="true" height="270"> 
    <e-gantt-columns>
        <e-gantt-column field="TaskID" headerText="Task ID" type="number" textAlign="Right" width="120"></e-gantt-column>
        <e-gantt-column field="TaskName" headerText="Task Name"  width="150"  headerTemplate="#customertemplate"></e-gantt-column>
        <e-gantt-column field="StartDate" headerText="Start Date"  width="150"></e-gantt-column>
        <e-gantt-column field="Duration" headerText="Duration"  width="150"></e-gantt-column>
    </e-gantt-columns>
</ejs-gantt>

<script>
document.getElementById('restore').onclick = function () {
    var savedProperties = JSON.parse(document.getElementById('Gantt').ej2_instances[0].getPersistData());
    var gridColumnsState = Object.assign([], document.getElementById('Gantt').ej2_instances[0].ganttColumns);
    savedProperties.columns.forEach(function (col) {
        var headerText = gridColumnsState.find(function (colColumnsState) { return colColumnsState.field === col.field; })['headerText'];
        var colTemplate = gridColumnsState.find(function (colColumnsState) { return colColumnsState.field === col.field; })['template'];
        var headerTemplate = gridColumnsState.find(function (colColumnsState) { return colColumnsState.field === col.field; })['headerTemplate'];
        col.headerText = 'Text Changed';
        col.template = colTemplate;
        col.headerTemplate = headerTemplate;
    });
        console.log(savedProperties);
        document.getElementById('Gantt').ej2_instances[0].TreeGrid.setProperties(savedProperties);
}
</script>
  
<script id="customertemplate" type="text/x-template">
    <span class="e-icons e-header" ></span>
    Task Name
</script>
  
<style>
    .e-Reply:before {
        content: '\e815';
    }
  
    .e-header:before {
        content: '\ea9a';
    }
</style>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.EditingData();	
    return View();
}