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