<SfButton OnClick="SetState">Set State</SfButton> //set the state
<SfButton OnClick="ResetState">Reset State</SfButton> //reset the state
<SfGrid ID="ParentGrid" DataSource="@Employees" Height="705px">
<GridTemplates>
<DetailTemplate>
@{
var employee = (context as EmployeeData);
<SfGrid ID="DetailGrid" @ref="DetailGridRef" DataSource="@Orders" Query="@(new Query().Where("EmployeeID", "equal", employee.EmployeeID))"
ShowColumnChooser="true" Toolbar=@ToolbarItems
EnablePersistence="true">
<GridColumns>
. . .
</GridColumns>
</SfGrid>
}
</DetailTemplate>
</GridTemplates>
<GridColumns>
. . .
</GridColumns>
</SfGrid>
@code{
SfGrid<Order> DetailGridRef; //Grid reference
public string _state; //for maintaining grid state
. . .
public async Task SetState()
{
await DetailGridRef.SetPersistData(_state); //set state using SetPersistData method
}
public async Task ResetState()
{
await DetailGridRef.ResetPersistData(); //reset the state
}
} |
<SfGrid ID="ParentGrid" @ref="ParentRef" DataSource="@Employees" Height="705px">
<GridEvents DetailDataBound="DetailDataBoundHandler" TValue="EmployeeData"></GridEvents>
<GridTemplates>
<DetailTemplate>
@{
var employee = (context as EmployeeData);
<SfGrid ID="DetailGrid" @ref="myComponents[id]" DataSource="@Orders" Query="@(new Query().Where("EmployeeID", "equal", employee.EmployeeID))"
ShowColumnChooser="true" Toolbar=@ToolbarItems
EnablePersistence="true">
<GridEvents OnActionComplete="ActionCompletedHandler" BeforeOpenColumnChooser="BeforeOpenColumnChooserHandler" TValue="Order"></GridEvents>
<GridColumns>
. . .
</GridColumns>
</SfGrid>
}
</DetailTemplate>
</GridTemplates>
<GridColumns>
. . .
</GridColumns>
</SfGrid>
@code{
SfGrid<EmployeeData> ParentRef { get; set; } //parent Grid reference
public string _state;
public string id { get; set; }
private Dictionary<string, SfGrid<Order>> myComponents = new Dictionary<string, SfGrid<Order>>(); //for storing Detail Grid reference dynamically
public int? CId { get; set; }
. . .
public void BeforeOpenColumnChooserHandler(ColumnChooserEventArgs Args)
{
CId = int.Parse(this.id); //get id while opening column chooser, using this id we have reopened the detail Grid
}
public void DetailDataBoundHandler(DetailDataBoundEventArgs<EmployeeData> args)
{
id = args.Data.EmployeeID.ToString(); //set detail Grid id when opening detail Grid
}
public async Task ActionCompletedHandler(ActionEventArgs<Order> args)
{
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.ColumnState))
{
var RowVal = Employees.Where(or => or.EmployeeID == CId).FirstOrDefault(); //from the id from column chooser, we have filtered the parent data
await myComponents[CId.ToString()].SetPersistData(_state); //persist state in corresponding detail grid reference
await ParentRef.DetailCollapseAll(); //collapse all detail grid's using parent grid reference
await ParentRef.DetailExpandCollapseRow(RowVal); //expand the previously opened column chooser detail Grid
}
}
} |
[Index.js]
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.Data
@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.Navigations
@inject IJSRuntime JSRuntime
<SfGrid ID="ParentGrid" @ref="ParentRef" DataSource="@Employees" Height="705px">
<GridEvents DetailDataBound="DetailDataBoundHandler" TValue="EmployeeData"></GridEvents>
<GridTemplates>
<DetailTemplate>
@{
var employee = (context as EmployeeData);
<SfGrid @ref="myComponents[id]" @attributes="@(new Dictionary<string, object>(){ { "something", @employee.EmployeeID }})"
DataSource="@Orders" Query="@(new Query().Where("EmployeeID", "equal", employee.EmployeeID))"
Toolbar=@ToolbarItems ShowColumnChooser="true">
<GridEvents OnActionBegin="Begin" OnActionComplete="ActionCompletedHandler"
OnToolbarClick="ToolbarClickHandler" TValue="Order"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="OrderID" Visible="@Fields["OrderID"]" Width="110"> </GridColumn>
<GridColumn Field=@nameof(Order.CustomerName) HeaderText="CustomerName" Visible="@Fields["CustomerName"]" Width="110"></GridColumn>
<GridColumn Field=@nameof(Order.ShipCountry) HeaderText="ShipCountry" Visible="@Fields["ShipCountry"]" Width="110"></GridColumn>
</GridColumns>
</SfGrid>
}
</DetailTemplate>
</GridTemplates>
<GridColumns>
. ..
</GridColumns>
</SfGrid>
@code{
SfGrid<EmployeeData> ParentRef { get; set; } //parent Grid reference
IDictionary<string, bool> Fields = new Dictionary<string, bool>(); //for storing the Visibility value
public string id { get; set; }
private Dictionary<string, SfGrid<Order>> myComponents = new Dictionary<string, SfGrid<Order>>(); //for storing Detail Grid reference dynamically
//public int? CId { get; set; }
string ColChoosenID { get; set; }
public string[] ToolbarItems = new string[] { "ColumnChooser" };
protected override void OnInitialized()
{
Fields.Add("OrderID", true);
Fields.Add("CustomerName", false);
Fields.Add("ShipCountry", true); //stored Visibility value at initial
}
. . .
public async Task Begin(ActionEventArgs<Order> args)
{
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.ColumnState)) //checking requesttype
{
var hiddenCount = args.HiddenColumns.Count(); //get hidden columns count
var visibleCount = args.VisibleColumns.Count(); //get visible columns count
var totCount = Fields.Count();
for (var i = 0; i < hiddenCount; i++)
{
Fields[args.HiddenColumns[i].Field] = args.HiddenColumns[i].Visible; //setting Visibility values in Dictionary object
}
for (var i = 0; i < visibleCount; i++)
{
Fields[args.VisibleColumns[i].Field] = args.VisibleColumns[i].Visible; //setting Visibility values in Dictionary object
}
}
}
public void DetailDataBoundHandler(DetailDataBoundEventArgs<EmployeeData> args)
{
id = args.Data.EmployeeID.ToString(); //set detail Grid id when opening detail Grid
}
public async Task ActionCompletedHandler(ActionEventArgs<Order> args)
{
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.ColumnState))
{
var RowVal = Employees.Where(or => or.EmployeeID == int.Parse(ColChoosenID)).FirstOrDefault();
await ParentRef.DetailCollapseAll(); //collapse all detail grid's using parent grid reference
await ParentRef.DetailExpandCollapseRow(RowVal); //expand the previously opened column chooser detail Grid
}
}
public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args)
{
string[] words = args.Item.Id.Split('_'); //get detail grid id
ColChoosenID = await JSRuntime.InvokeAsync<string>("getValue", words[0]); //make a interop call using detail grid id
}
} |
[Interop.js]
function getValue(id) {
var uniqueValueDetailGrid = document.getElementById(id).getAttribute("something");
return uniqueValueDetailGrid; //return id
} |
<head>
. . .
<link rel='nofollow' href="_content/Syncfusion.Blazor/styles/fabric.css" rel="stylesheet" />
<script src="~/Interop.js"></script>
</head> |
. . .
@inject IJSRuntime JSRuntime
<SfGrid ID="ParentGrid" @ref="ParentRef" DataSource="@Employees" Height="705px">
<GridEvents DetailDataBound="DetailDataBoundHandler" TValue="EmployeeData"></GridEvents>
<GridTemplates>
<DetailTemplate>
@{
var employee = (context as EmployeeData);
<SfGrid @ref="myComponents[id]" @attributes="@(new Dictionary<string, object>(){ { "something", @employee.EmployeeID }})"
DataSource="@Orders" Query="@(new Query().Where("EmployeeID", "equal", employee.EmployeeID))"
Toolbar=@ToolbarItems ShowColumnChooser="true">
<GridEvents OnActionComplete="ActionCompletedHandler"
OnToolbarClick="ToolbarClickHandler" TValue="Order"></GridEvents>
<GridColumns>
. . .
</GridColumns>
</SfGrid>
}
</DetailTemplate>
</GridTemplates>
<GridColumns>
. . .
</GridColumns>
</SfGrid>
@code{
SfGrid<EmployeeData> ParentRef { get; set; } //parent Grid reference
IDictionary<string, bool> Fields = new Dictionary<string, bool>(); //for storing the Visibility value
public string id { get; set; }
private Dictionary<string, SfGrid<Order>> myComponents = new Dictionary<string, SfGrid<Order>>(); //for storing Detail Grid reference dynamically
string ColChoosenID { get; set; }
public string _state;
. . .
public async Task DetailDataBoundHandler(DetailDataBoundEventArgs<EmployeeData> args)
{
id = args.Data.EmployeeID.ToString(); //set detail Grid id when opening detail Grid
var state = await JSRuntime.InvokeAsync<string>("getStateValue"); //calling interop function for getting the state which stored in browser local storage
if(state != null)
{
await myComponents[id].SetPersistData(state); //set the previous state
}
}
public async Task ActionCompletedHandler(ActionEventArgs<Order> args)
{
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.ColumnState))
{
var RowVal = Employees.Where(or => or.EmployeeID == int.Parse(ColChoosenID)).FirstOrDefault();
_state = await myComponents[ColChoosenID].GetPersistData(); //get state of the Grid
await JSRuntime.InvokeAsync<string>("setStateValue", _state); //send the state to interop function for storing the state in browser local storage
await ParentRef.DetailCollapseAll(); //collapse all detail grid's using parent grid reference
await ParentRef.DetailExpandCollapseRow(RowVal); //expand the previously opened column chooser detail Grid
}
}
public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args)
{
string[] words = args.Item.Id.Split('_');
ColChoosenID = await JSRuntime.InvokeAsync<string>("getValue", words[0]); //make a interop call using detail grid id
}
} |
[Interop.js]
function getValue(id) {
var uniqueValueDetailGrid = document.getElementById(id).getAttribute("something");
return uniqueValueDetailGrid;
}
function setStateValue(model) {
//set the Grid model.
window.localStorage.setItem('gridGrid', JSON.stringify(model)); //"gridGrid" is some id for storing the state.
}
function getStateValue() {
//get the Grid model.
var value = window.localStorage.getItem('gridGrid'); //"gridGrid" id for getting the state which is previous stored.
var model = JSON.parse(value);
return model;
} |