|
. . .
<SfButton OnClick="Refresh" CssClass="e-primary" Content="Refresh"></SfButton>
<SfGrid ID="Grid" @ref="DefaultGrid" AllowSelection="true" EnablePersistence="true" DataSource="@Employees" Height="315px">
<GridSelectionSettings Type="SelectionType.Multiple" PersistSelection="true"></GridSelectionSettings>
. . .
<GridColumn Type="ColumnType.CheckBox" Width="50"></GridColumn>
<GridColumn Field=@nameof(EmployeeData.EmployeeID) HeaderText="EmployeeID" IsPrimaryKey="true" Width="110"></GridColumn>
. . .
</GridColumns>
</SfGrid>
@code{
. . .
public void Refresh()
{
this.DefaultGrid.Refresh();
}
} |
|
<Syncfusion.Blazor.Buttons.SfButton @onclick="onclick">Refresh</Syncfusion.Blazor.Buttons.SfButton>
<SfGrid ID="ParentGrid" @ref="GridInstance" DataSource="@Employees">
<GridEvents DataBound="DataBound" TValue="EmployeeData"></GridEvents>
<GridTemplates>
<DetailTemplate>
...
</DetailTemplate>
</GridTemplates>
...
</SfGrid>
@code{
SfGrid<EmployeeData> GridInstance;
public void DataBound()
{
JsRuntime.InvokeAsync<object>("expandDetailRow");
}
public void onclick()
{
JsRuntime.InvokeAsync<object>("getExpandedRow");
GridInstance.Refresh();
}
...
}
[expandscript.js]
var rowInfo = [];
function expandDetailRow() {
var gridObj = document.getElementById("ParentGrid").ej2_instances[0];
if (rowInfo.length) {
for (var j = 0; j < rowInfo.length; j++) {
var rowIndex = parseInt(rowInfo[j].getAttribute("aria-rowindex")); //fetch the row index from aria-rowindex attribute
gridObj.detailRowModule.expand(rowIndex); //use the row index to expand the detailrow
}
}
}
function getExpandedRow() {
rowInfo = [];
var detailrows = document.getElementById("ParentGrid").querySelectorAll(".e-detailrowexpand"); //fetch the expanded row details
for (var i = 0; i < detailrows.length; i++) {
rowInfo.push(sf.grids.parentsUntil(detailrows[i],"e-row")); //store the details in rowInfo variable
}
} |
|
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.Data
@inject IJSRuntime Runtime
@using Syncfusion.Blazor.Buttons
<SfButton OnClick="OnClick">Refresh Grid</SfButton>
<SfGrid @ref="Grid" DataSource="@Employees" Height="315px">
<GridEvents DetailDataBound="DetailDataBound" DataBound="DataBound" TValue="EmployeeData"></GridEvents>
<GridTemplates>
<DetailTemplate>
@{
var employee = (context as EmployeeData);
<SfGrid DataSource="@Orders" Query="@(new Query().Where("EmployeeID", "equal", employee.EmployeeID))">
<GridColumns>
. ..
</GridColumns>
</SfGrid>
}
</DetailTemplate>
</GridTemplates>
<GridColumns>
<GridColumn Field=@nameof(EmployeeData.EmployeeID) IsPrimaryKey="true" HeaderText="First Name" Width="110"> </GridColumn>
. ..
</GridColumns>
</SfGrid>
@code{
SfGrid<EmployeeData> Grid;
. ..
public bool firstrender { get; set; } = true;
public async Task DataBound()
{
if (firstrender)
{
var dotNetReference = DotNetObjectReference.Create(this); // create dotnet ref
await Runtime.InvokeAsync<string>("detail", dotNetReference); // send the dotnet ref to JS side
firstrender = false;
}
foreach (var a in ExpandedRows)
{
var PKIndex = await Grid.GetRowIndexByPrimaryKey(a.Key);
await Grid.DetailExpandCollapseRow((EmployeeData)Grid.CurrentViewData.ElementAt(Convert.ToInt32(PKIndex))); //Expand the already expnaded detailrows
}
}
IDictionary<int, EmployeeData> ExpandedRows = new Dictionary<int, EmployeeData>();
public void DetailDataBound(DetailDataBoundEventArgs<EmployeeData> args)
{
if (!ExpandedRows.ContainsKey(args.Data.EmployeeID))
{
ExpandedRows.Add(args.Data.EmployeeID, args.Data); //add the expanded rows to dictionary
}
}
[JSInvokable("DetailCollapse")] // method called from JS when collapse is done
public void DetailRowCollapse(string CollapseIndex)
{
EmployeeData CollapseRow = (EmployeeData)Grid.CurrentViewData.ElementAt(Convert.ToInt32(CollapseIndex));
ExpandedRows.Remove(CollapseRow.EmployeeID); //Remove the collapsed row from expanded dictionary
}
public void OnClick()
{
Grid.Refresh();
}
}
|
|
[detailexpand.js] |