Editing
Since 0.1.0-alpha.24Editing and adding rows under others
A tree list edits as a DataGrid does: set EditMode and handle OnSave. To add a row under another, set NewChild and put an AddChildAction in the ActionColumn.
Static
Preview
Code
EditPlan.razor
IzzyBlazor.Site.Examples.DataGrid
@* A plan edited where it stands: a phase's row adds a task under it, the toolbar's Add a phase,
Move, or a drag by its handle, puts a task under another phase, and deleting a phase takes its tasks with it. Hours roll up
to the phases. *@
<TreeList TItem="PlanItem" Items="_plan" ItemKey="p => p.Id.ToString()" ItemParentKey="p => p.ParentId?.ToString()"
ExpandAll Caption="Plan" QueryPrefix="plan-edit-" EditMode="DataGridEditMode.Row"
OnSave="Save" NewItem="NewPhase" NewChild="NewTask" OnReparent="Reparent" MoveByDrag OnDelete="Delete" Grow>
<Columns>
<PropertyColumn Property="p => p.Name" Title="Task" />
<PropertyColumn Property="p => p.Owner" Width="10.Rem" />
<PropertyColumn Property="p => p.Hours" RollUp="DataAggregate.Sum" Width="7.Rem" />
<ActionColumn>
<AddChildAction />
<EditAction />
<MoveAction Overflow="RowActionOverflow.Always" />
<DeleteAction Overflow="RowActionOverflow.Always" />
</ActionColumn>
</Columns>
</TreeList>
{
[CascadingParameter] private HttpContext? Http { get; set; }
private List<PlanItem> _plan = [];
// Stands in for your database, with a copy of the rows for each visitor to this site.
protected override void OnInitialized() => _plan = ExampleRows.For(Http, "tree-edit", () => Estimate.All
.Where(w => w.Id <= 8)
.Select(w => new PlanItem { Id = w.Id, ParentId = w.ParentId, Name = w.Task, Owner = w.Owner, Hours = w.Hours })
.ToList());
private void Save(DataGridSave<PlanItem> save)
{
if (save.IsNew)
{
_plan.Add(save.Item);
return;
}
save.CopyTo(_plan.First(p => p.Id == save.Item.Id));
}
// The row's new parent, and its place among the rows already there.
private void Reparent(TreeListMove<PlanItem> move)
{
var item = _plan.First(p => p.Id == move.Item.Id);
_plan.Remove(item);
item.ParentId = move.Parent?.Id;
var siblings = _plan.Where(p => p.ParentId == item.ParentId).ToList();
_plan.Insert(move.Index < siblings.Count ? _plan.IndexOf(siblings[move.Index]) : _plan.Count, item);
}
private PlanItem NewPhase() => new() { Id = NextId() };
private PlanItem NewTask(PlanItem phase) => new() { Id = NextId(), ParentId = phase.Id, Owner = phase.Owner };
private int NextId() => _plan.Count == 0 ? 1 : _plan.Max(p => p.Id) + 1;
// A row and everything under it.
private void Delete(DataGridDelete<PlanItem> delete)
{
var gone = new HashSet<int> { delete.Item.Id };
var more = true;
while (more)
{
more = false;
foreach (var item in _plan.Where(p => p.ParentId is { } parent && gone.Contains(parent)))
{
more |= gone.Add(item.Id);
}
}
_plan.RemoveAll(p => gone.Contains(p.Id));
}
}PlanItem.cs
using System.ComponentModel.DataAnnotations;
namespace IzzyBlazor.Site.Examples.TreeList;
/// <summary>A phase or task of a plan, for the TreeList editing example: settable, as a posted form fills it.</summary>
public sealed class PlanItem
{
public int Id { get; set; }
public int? ParentId { get; set; }
[Required, StringLength(60)]
public string Name { get; set; } = "";
public string Owner { get; set; } = "";
[Range(0, 400)]
public decimal Hours { get; set; }
}NewChild gets the row the new one goes under, so it can set the parent's id. The new row opens under it, which opens if it was closed, and OnSave gets it with IsNew set.
NewItem adds a top row, from the toolbar's Add. Deleting a row is yours to handle, so decide there what happens to the rows under it: this example deletes them too.
To move a row under another, add a MoveAction and handle OnReparent. Its dialog offers every row but the moving one and its branch, under Top level; OnReparent gets the new parent, or none, and the row's place after its new siblings.
To drag rows too, set MoveByDrag: each row gets a handle, and a drop on a row's top or bottom edge puts it before or after, its middle under it. A drop moves the row at once, on a static page too; pressing a handle opens the Move dialog.