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.
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();
}
If the enablePersistence
property is set to true, the grid 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 Grid model.
var value = window.localStorage.getItem('gridGrid'); //"gridGrid" is component name + component id.
var model= JSON.parse(model);
//set the Grid model.
window.localStorage.setItem('gridGrid', JSON.stringify(model)); //"gridGrid" is component name + component id.