@page "/TreeGrid"
@using Syncfusion.Blazor.TreeGrid
@using Syncfusion.Blazor.Grids
<h1>TreeGridh1>
@if (DataSource == null)
{
<p>Loading ... please wait.....p>
}
else
{
<SfTreeGrid @ref="treeGrid" DataSource="@DataSource" TValue="TreeGridItem"
DataSourceChanged="TreeGridItemUpdated" IdMapping="Id" ParentIdMapping="ParentId" ExpandStateMapping="IsExpanded" TreeColumnIndex="1"
AllowFiltering="true" AllowPaging="true" AllowSorting="true" AllowRowDragAndDrop="true"
Toolbar="@(new List<string>() { "Add", "Edit", "Delete" })" Height="100%">
<TreeGridEvents TValue="TreeGridItem"
RowDropped="TreeGridItemDropped">TreeGridEvents>
<TreeGridPageSettings PageSize="3">TreeGridPageSettings>
<TreeGridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true" Mode="Syncfusion.Blazor.TreeGrid.EditMode.Dialog">TreeGridEditSettings>
<TreeGridFilterSettings Type="Syncfusion.Blazor.TreeGrid.FilterType.Menu">TreeGridFilterSettings>
<TreeGridColumns>
<TreeGridColumn Field="Id" HeaderText="Id" Width="80" IsPrimaryKey="true" Visible="true" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right">TreeGridColumn>
<TreeGridColumn Field="Name" HeaderText="Name" Width="240">TreeGridColumn>
<TreeGridColumn Field="ParentId" HeaderText="Pid" Width="240" Visible="true">TreeGridColumn>
TreeGridColumns>
SfTreeGrid>
<table>
<thead>
<th>Idth>
<th>Nameth>
<th>ParentIdth>
<th>IsExpandedth>
thead>
@foreach (var tgi in DataSource)
{
<tr>
<td>@tgi.Idtd>
<td>@tgi.Nametd>
<td>@tgi.ParentIdtd>
<td>@tgi.IsExpandedtd>
tr>
}
table>
}
@code {
[Parameter] public List
protected SfTreeGrid
protected override void OnInitialized()
{
DataSource = TreeGridItem.GetSampleData();
base.OnInitialized();
}
protected void TreeGridItemUpdated(IEnumerable
{
foreach (var tgi in args)
{
Console.WriteLine($"Updated: {tgi.Id} - {tgi.Name} - {tgi.ParentId}");
}
}
protected void TreeGridItemDropped(RowDragEventArgs
{
Console.WriteLine($"Dropped onto element at Index: {args.DropIndex}");
var ptgi = treeGrid.GetCurrentViewRecords().ElementAtOrDefault((int)args.DropIndex);
Console.WriteLine($"Dropped onto Item with Id {ptgi.Id} - Name: {ptgi.Name} - ParentId: {ptgi.ParentId}");
foreach (var tgi in args.Data)
{
Console.WriteLine($"Dropped item with Id {tgi.Id} - Name: {tgi.Name} - ParentId: {tgi.ParentId}");
}
}
public class TreeGridItem
{
public int Id { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
public bool IsParent { get; set; } = false;
public bool IsExpanded { get; set; } = false;
public static List
{
return new List
{
new TreeGridItem
{
Id = 1,
Name = "Parent",
IsParent = true,
IsExpanded = true }
,
new TreeGridItem
{
Id = 2,
Name = "Child1",
ParentId = 1,
IsParent = false,
IsExpanded = true }
,
new TreeGridItem
{
Id = 3,
Name = "Child2",
ParentId = 1,
IsParent = false,
IsExpanded = true }
,
new TreeGridItem
{
Id = 4,
Name = "Parent 2",
IsParent = false,
IsExpanded = true }
};
}
}
}