State Persistence in Grid Control

14 Nov 20225 minutes to read

State persistence refers to the Grid’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 grid’s model object in the local storage when the enablePersistence is defined as true.

Restore initial Grid state

When the enablePersistence property is set to true, the Grid will keep its state even if the page is reloaded. In some cases, you may be required to retain the Grid in its initial state. The Grid will not retain its initial state now since the enablePersistence property has been enabled.

You can achieve this by destroying the grid after disabling the enablePersistence property and clearing the local storage data, as shown in the sample below.

<ejs-button id="restore" content="Restore to initial state" isPrimary="true"></ejs-button>
<ejs-grid id="Grid" dataSource="@ViewBag.datasource" enablePersistence="true" allowPaging="true" allowFiltering="true" height="270"> 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" type="number" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="EmployeeID" headerText="Customer ID"  width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City"  width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name"  width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
document.getElementById('restore').onclick = function () {
    document.getElementById('Grid').ej2_instances[0].enablePersistence = false;
    window.localStorage.setItem("gridGrid", "");
    document.getElementById('Grid').ej2_instances[0].destroy();
     //reloads the page
    location.reload();
}
</script>
public IActionResult Index()
 {
    var orders = OrderDetails.GetAllRecords();
    ViewBag.datasource = orders;            
    return View();
 }

Maintaining custom query in a persistent state

The grid does not maintain the query params after page load event when the enablePersistence is set to true. This is because the grid refreshes its query params for every page load. You can maintain the custom query params by resetting the addParams method in actionBegin event.

<ejs-grid id="Grid" dataSource="@ViewBag.datasource" enablePersistence="true" actionBegin="actionBegin" allowFiltering="true" height="270"> 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" type="number" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="EmployeeID" headerText="Customer ID"  width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City"  width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name"  width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>

function actionBegin(args) { 
    this.query.addParams('$filter', 'EmployeeID eq 1');
}

</script>
public IActionResult Index()
 {
    var orders = OrderDetails.GetAllRecords();
    ViewBag.datasource = orders;            
    return View();
 }

Add a new column in persisted columns list

The Grid columns can be persisted when the enablePersistence property is set to true. If you want to add the new columns with the existing persist state, you can use the Grid inbuilt method such as column.push and call the refreshColumns() method for UI changes. Refer to the following sample for more information.

<ejs-button id="add" content="Add Columns" isPrimary="true"></ejs-button>
<ejs-grid id="Grid" dataSource="@ViewBag.Datasource" enablePersistence="true" allowPaging="true" height="270"> 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" type="number" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="EmployeeID" headerText="Customer ID"  width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City"  width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name"  width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
document.getElementById('add').onclick = function () {
    var obj = { field: "Freight", headerText: 'Freight', width: 120 }
    document.getElementById('Grid').ej2_instances[0].columns.push(obj); //you can add the columns by using the Grid columns method
    document.getElementById('Grid').ej2_instances[0].refreshColumns();
}
</script>
public IActionResult Index()
 {
    var orders = OrderDetails.GetAllRecords();
    ViewBag.Datasource = orders;            
    return View();
 }