|
<SfTreeGrid @ref="TreeGrid" DataSource="@TreeGridData" IdMapping="TaskId" ParentIdMapping="ParentId"
TreeColumnIndex="1" Toolbar="@ToolbarItems"
AllowPaging="true" >
<TreeGridEvents TValue="TreeData" RowSelected="RowSelectHandler" OnActionComplete="ActionComplete"
></TreeGridEvents>
<TreeGridColumns>
<TreeGridColumn Field="TaskId" HeaderText="Task ID" IsPrimaryKey="true" Width="80" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>
<TreeGridColumn Field="TaskName" HeaderText="Task Name" Width="160"></TreeGridColumn>
<TreeGridColumn HeaderText="Add New Record" Width="80">
<Template>
<SfSplitButton IconCss="e-btn-icons e-paste">
<SplitButtonEvents ItemSelected="ItemSelected">
</SplitButtonEvents>
<DropDownMenuItems>
<DropDownMenuItem Text="Add Above" Id="Above"></DropDownMenuItem>
<DropDownMenuItem Text="Add Below" Id="Below"></DropDownMenuItem>
<DropDownMenuItem Text="Add Child" Id="Child"></DropDownMenuItem>
</DropDownMenuItems>
</SfSplitButton>
</Template>
</TreeGridColumn>
</SfTreeGrid>
@code{
SfTreeGrid<TreeData> TreeGrid;
public List<TreeData> TreeGridData { get; set; }
public List<string> ToolbarItems { get; set; }
public Syncfusion.Blazor.TreeGrid.RowPosition position { get; set; } = RowPosition.Below;
public double PreviousSelectedIndex = -1;
public int value = 20;
public static int? Pkey { get; set; }
protected override void OnInitialized()
{
this.ToolbarItems = new List<string>() { "Add", "Delete", "Update", "Cancel" };
this.TreeGridData = TreeData.GetSelfDataSource().ToList();
}
public void RowSelectHandler(RowSelectEventArgs<TreeData> args)
{
PreviousSelectedIndex = args.RowIndex; // saving the row index
}
public async void ActionComplete(ActionEventArgs<TreeData> args)
{
if (args.RequestType.ToString() == "Save")
{
await Task.Delay(100);
Pkey = args.Data.TaskId; //get primary key value of newly added record
var index = this.TreeGrid.GetRowIndexByPrimaryKey(Pkey);
this.TreeGrid.SelectRow(index.Result);
}
}
private void ItemSelected(MenuEventArgs args)
{
++value;
var data = new TreeData()
{
TaskId = value,
TaskName = "Parent Task 1",
Duration = 10,
Progress = 70,
Priority = "Critical",
Resources = 1
};
if (args.Item.Text == "Add Above")
{
this.TreeGrid.AddRecord(data, PreviousSelectedIndex, RowPosition.Above);
}
if (args.Item.Text == "Add Below")
{
this.TreeGrid.AddRecord(data, PreviousSelectedIndex, RowPosition.Below);
}
if (args.Item.Text == "Add Child")
{
this.TreeGrid.AddRecord(data, PreviousSelectedIndex, RowPosition.Child);
}
}
}
|