Selection
Since 0.1.0-alpha.24Checkboxes select rows, and checking a row checks the rows under it.
Checking a branch
Checking a row checks the rows under it, and a row with some of them checked shows a mixed box. Set SelectionMode="SelectionMode.Multiple"; to make each box its own, set Cascade to false.
Static
Preview
Code
@* Choose what to back up: checking a folder checks everything in it, opened or not. Back up posts
the selection, and Selection.ApplyTree finds every file a checked folder stands for. *@
<form method="post" @onsubmit="BackUp" @formname="tree-backup" style="width: 100%">
<AntiforgeryToken />
<Stack Gap="3">
<TreeList Items="_files" ItemKey="f => f.Id.ToString()" ItemParentKey="f => f.FolderId?.ToString()" ItemIcon="Icon"
ItemExpanded="f => f.FolderId is null" SelectionMode="SelectionMode.Multiple" @bind-Selection="_selection"
QueryPrefix="backup-" Caption="Files to back up" Grow>
<SelectionActions>
<Button Submit Size="ControlSize.Sm" Variant="ButtonVariant.Ghost" Accent="Accent.Neutral" StartIcon="Lucide.HardDriveUpload">Back up</Button>
</SelectionActions>
<Columns>
<PropertyColumn Property="f => f.Name" />
<PropertyColumn Property="f => f.Modified" Format="MMM d, yyyy" Width="9.Rem" />
<TemplateColumn Title="Size" Align="CellAlign.End" Width="7.Rem">Size(context.Bytes)</TemplateColumn>
</Columns>
</TreeList>
<TextBlock Size="TextSize.Sm" Color="Color.Muted" role="status">_status</TextBlock>
</Stack>
</form>
{
// Static pages: the selection posts under the tree list's SelectionName ("{QueryPrefix}selected").
[SupplyParameterFromForm(FormName = "tree-backup", Name = "backup-selected")]
private Selection? Posted { get; set; }
// Interactive pages: bound to the tree list.
private Selection? _selection;
private string? _status;
protected override void OnInitialized() => _selection = Posted;
private void BackUp()
{
var files = (Posted ?? _selection ?? Selection.None).ApplyTree(_files.AsQueryable(), f => f.Id, f => f.FolderId)
.Where(f => f.Bytes != null).ToList();
_status = files.Count == 0
? "Choose the files or folders to back up first."
: $"Backing up {files.Count} files, {Size(files.Sum(f => f.Bytes))}.";
_selection = Selection.None;
}
private static IconData? Icon(StoredFile file, bool open) =>
file.Bytes is not null ? Lucide.FileText : open ? Lucide.FolderOpen : Lucide.Folder;
private static string Size(long? bytes) => bytes switch
{
null => "",
< 1024 * 1024 => $"{bytes / 1024.0:0} KB",
_ => $"{bytes / (1024.0 * 1024):0.0} MB",
};
}A checked row stands for its whole branch, opened or not: rows under it come in checked when they open, and unchecking one unchecks the rows above it. A row whose rows are all checked is checked too.
To find every row the selection means, call selection.ApplyTree with the key and the parent key. It adds the rows under each checked row, a level at a time.
While filtering, the muted rows above a match have no box, so the selection holds only matches.