State Persistence

14 Nov 20224 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.

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

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

    col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("CustomerID").HeaderText("Customer Name").Width("170").Add();
    col.Field("OrderDate").HeaderText("Order Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

}).AllowPaging(true).PageSettings(page => page.PageCount(5)).Render()

<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 the ActionBegin event.

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

    col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("CustomerID").HeaderText("Customer Name").Width("170").Add();
    col.Field("OrderDate").HeaderText("Order Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

}).AllowPaging().PageSettings(page => page.PageCount(5)).Render()

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

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

@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).EnablePersistence(true).Columns(col =>
{
    col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("CustomerID").HeaderText("Customer Name").Width("170").Add();
    col.Field("OrderDate").HeaderText("Order Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

}).AllowPaging(true).PageSettings(page => page.PageCount(5)).Render()

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