State Persistence Feature

21 Dec 20226 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.

@Html.EJS().Button("add").Content("Add Columns").IsPrimary(true).Render()
@Html.EJS().Button("remove").Content("Remove Columns").IsPrimary(true).Render()

@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.DataSource).DataBound("onBound").EnablePersistence(true).Columns(col =>
{
    col.Field("TaskID").HeaderText("Task ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("TaskName").HeaderText("Task Name").Width("170").Add();
    col.Field("StartDate").HeaderText("Start Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Duration").HeaderText("Duration").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

}).Render()

<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 Grid 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.

@Html.EJS().Button("restore").Content("Restore").IsPrimary(true).Render()

@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.dataSource).EnablePersistence(true).Columns(col =>
{

    col.Field("TaskID").HeaderText("Task ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("TaskName").HeaderText("Task Name").Width("170").HeaderTemplate("#customertemplate").Add();
    col.Field("StartDate").HeaderText("Start Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Duration").HeaderText("Duration").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

}).Render()

<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();
}