Selection
Since 0.1.0-alpha.20Selecting rows
Row selection adds a checkbox to each row, so the user can pick rows to act on. Set SelectionMode="SelectionMode.Multiple" and an ItemKey. For a radio button on each row instead, set SelectionMode.Single.
Static
Preview
Code
@* Select orders on any page, or every order the filters match with the header checkbox. While any
are selected, the bar of SelectionActions takes the toolbar's place; Archive posts the selection,
and Selection.Apply finds the orders it means. *@
Rows = IzzyBlazor.Site.Examples.Table.Orders
<form method="post" @onsubmit="Archive" @formname="grid-archive" style="width: 100%">
<AntiforgeryToken />
<Stack Gap="3">
<DataGrid Items="Rows.All" PageSize="5" Caption="Orders to archive" QueryPrefix="pick-" Grow
ItemKey="o => o.Number.ToString()" RowLabel='o => $"order {o.Number}"'
SelectionMode="SelectionMode.Multiple" SelectAllMode="SelectAllMode.All" @bind-Selection="_selection">
<ToolbarContent>
<ToolbarButton StartIcon="Lucide.Plus" Position="ToolbarPosition.End" Variant="ButtonVariant.Outline">New order</ToolbarButton>
</ToolbarContent>
<SelectionActions>
<Button Submit Size="ControlSize.Sm" Variant="ButtonVariant.Ghost" Accent="Accent.Neutral" StartIcon="Lucide.Archive">Archive</Button>
</SelectionActions>
<Columns>
<PropertyColumn Property="o => o.Number" Title="Order" Align="CellAlign.Start" Width="6.Rem" />
<PropertyColumn Property="o => o.Customer" Filterable />
<PropertyColumn Property="o => o.Status" Filterable Width="9.Rem" />
<PropertyColumn Property="o => o.Total" Format="C" Width="8.Rem" />
</Columns>
</DataGrid>
<TextBlock Size="TextSize.Sm" Color="Color.Muted" role="status">_status</TextBlock>
</Stack>
</form>
{
// Static pages: the selection posts under the grid's SelectionName ("{QueryPrefix}selected").
[SupplyParameterFromForm(FormName = "grid-archive", Name = "pick-selected")]
private Selection? Posted { get; set; }
// Interactive pages: bound to the grid.
private Selection? _selection;
private string? _status;
protected override void OnInitialized() => _selection = Posted;
private void Archive()
{
var orders = (Posted ?? _selection ?? Selection.None).Apply(Rows.All.AsQueryable(), o => o.Number.ToString()).ToList();
_status = orders.Count switch
{
0 => "Select the orders to archive first.",
1 => $"Archived order #{orders[0].Number}.",
_ => $"Archived {orders.Count} orders.",
};
_selection = Selection.None;
}
}While rows are selected, the buttons in SelectionActions take the toolbar's place, so the rows don't move.
In a form, the checked rows post their keys, so selection works with JavaScript off. Read them as a Selection with [SupplyParameterFromForm], or bind @bind-Selection on interactive pages, then call its Apply to find the rows.
The header checkbox selects the rows on the current page. To make it select every row the filters match, on any page, set SelectAllMode="SelectAllMode.All".
Selecting a row by a click
To select a row by clicking it, set SelectOnClick. With SelectionMode.Single the rows have no radio buttons, and the selected row shows by its tint. A click or Space selects a row, and Space on the selected row clears it.
Always Interactive
Preview
Code
@* Click an order to see it below: one row at a time, by a click or Space, with no radio buttons. *@
IzzyBlazor.Site.Examples.Table
<Stack Gap="4">
<DataGrid Items="Orders.All" ItemKey="o => o.Number.ToString()" RowLabel='o => $"order {o.Number}"' PageSize="5"
Caption="Orders to review" QueryPrefix="pick-one-" SelectionMode="SelectionMode.Single" SelectOnClick
@bind-Selection="_selection" Grow>
<Columns>
<PropertyColumn Property="o => o.Number" Title="Order" Align="CellAlign.Start" Width="6.Rem" />
<PropertyColumn Property="o => o.Customer" Sortable />
<PropertyColumn Property="o => o.Status" Width="9.Rem" />
<PropertyColumn Property="o => o.Total" Format="C" Sortable Width="8.Rem" />
</Columns>
</DataGrid>
<Divider Label="Order" />
if (Picked is { } order)
{
<Stack Gap="1">
<Heading Level="3" Size="TextSize.Lg">Order #order.Number</Heading>
<TextBlock>order.Customer, order.Date.ToString("MMMM d"): order.Total.ToString("C"), order.Status.ToString().ToLowerInvariant().</TextBlock>
</Stack>
}
else
{
<TextBlock Color="Color.Muted">Pick an order to see it here.</TextBlock>
}
</Stack>
{
private Selection? _selection;
private Order? Picked => _selection?.Keys is [var key] ? Orders.All.FirstOrDefault(o => o.Number.ToString() == key) : null;
}Clicks on the row's links, buttons and fields stay theirs. With SelectionMode.Multiple the checkboxes stay: a click selects only that row, Ctrl+click adds or removes one, and Shift+click selects a range.
It needs IzzyBlazor's script. With JavaScript off, each row shows its radio button again. A row with a RowHref opens its link on a click, so it can't take both.