Selection
Since 0.1.0-alpha.21Checkboxes or a single choice. The keys post with a form or bind to C#.
Checkboxes
To give each item a checkbox, set SelectionMode to Multiple and ItemKey. In a form the checked keys post under SelectionName; on an interactive page, bind them with @bind-Selection.
Static
Preview
Code
@* Checkboxes in a form: checking a parent checks its branch, and Save posts every checked key,
parents included, so it works on a static page. *@
<form method="post" @onsubmit="Save" @formname="tree-view-permissions" data-enhance style="width: 100%; max-width: 22rem">
<AntiforgeryToken />
<Stack Gap="3">
<TreeView Items="_areas" ItemChildren="a => a.Children" ItemKey="a => a.Code" ItemText="a => a.Name"
SelectionMode="SelectionMode.Multiple" SelectionName="granted" @bind-Selection="_granted" Label="Permissions" />
<Row Gap="3" Align="Align.Center" Wrap>
<Button Submit>Save</Button>
<TextBlock Size="TextSize.Sm" Color="Color.Muted" role="status">_status</TextBlock>
</Row>
</Stack>
</form>
{
// Static pages: the checked keys post as "granted".
[SupplyParameterFromForm(FormName = "tree-view-permissions", Name = "granted")]
private Selection? Posted { get; set; }
// Interactive pages: bound to the boxes.
private Selection? _granted = Selection.Of("orders.view");
private string? _status;
private readonly Area[] _areas =
[
new("orders", "Orders", [new("orders.view", "View orders", []), new("orders.refund", "Refund orders", [])]),
new("products", "Products",
[
new("products.view", "View products", []),
new("products.edit", "Edit products", [new("products.price", "Change prices", []), new("products.stock", "Change stock", [])]),
]),
new("settings", "Settings", []),
];
protected override void OnInitialized() => _granted = Posted ?? _granted;
private void Save()
{
var keys = (Posted ?? _granted)?.Keys ?? [];
_status = keys.Length == 0 ? "No permissions granted." : $"Saved {keys.Length} permissions: {string.Join(", ", keys)}.";
}
}Checking a parent checks its branch, and a parent with part of its branch checked shows a mixed box. To make each box its own, set Cascade to false.
The selection holds every checked key, parents included.
One choice
To choose one item, set SelectionMode to Single. A click, Space or Enter selects it, and the branch holding it starts open.
Static
Preview
Chosen: Laptops
Code
@* One choice: a click, Space or Enter selects an item, and its branch opens on the way back. *@
<Stack Gap="3" MaxWidth="20.Rem" Grow>
<TreeView Items="_categories" ItemChildren="c => c.Children" ItemKey="c => c.Name" ItemText="c => c.Name"
SelectionMode="SelectionMode.Single" @bind-Selection="_category" Label="Category" />
<TextBlock Size="TextSize.Sm" Color="Color.Muted">Chosen: (_category?.Keys?.FirstOrDefault() ?? "none")</TextBlock>
</Stack>
{
private Selection? _category = Selection.Of("Laptops");
private readonly Kind[] _categories =
[
new("Computers", [new("Laptops", []), new("Desktops", []), new("Monitors", [])]),
new("Phones", [new("Cases", []), new("Chargers", [])]),
new("Audio", [new("Headphones", []), new("Speakers", [])]),
];
}