Reordering rows
Since 0.1.0-alpha.20Users put rows in their own order by dragging a handle or from its menu, and the grid posts or binds the new order.
Reordering rows, and saving the order
Each row gets a drag handle at its start, which also opens a menu of moves. Set ReorderableRows and an ItemKey, and give the grid every row in Items.
Static
Preview
Code
@* Drag a handle, or open its menu, to put the backlog in priority order. On a static page the grid
posts with its form: Order holds the posted keys and any menu move, and the grid shows them.
The columns don't sort: the order is the data. *@
<Form Model="this" FormName="data-grid-backlog" OnValidSubmit="Save">
<Stack Gap="3" Grow>
<DataGrid @bind-Items="_tasks" ItemKey="t => t.Id" RowLabel="t => t.Title" Caption="Backlog"
ReorderableRows OrderName="order" Order="Order">
<Columns>
<PropertyColumn Property="t => t.Title" />
<PropertyColumn Property="t => t.Owner" Width="10.Rem" />
<PropertyColumn Property="t => t.Points" Title="Points" Width="6.Rem" />
</Columns>
</DataGrid>
<FormActions><Button Submit>Save order</Button></FormActions>
if (_saved is not null)
{
<TextBlock Size="TextSize.Sm" Color="Color.Muted">_saved</TextBlock>
}
</Stack>
</Form>
{
[SupplyParameterFromForm(FormName = "data-grid-backlog", Name = "order")]
private ListOrder? Order { get; set; }
private IReadOnlyList<BacklogTask> _tasks =
[
new("1", "Sign-in with passkeys", "Ada", 5),
new("2", "Export to PDF", "Grace", 3),
new("3", "Dark mode for email", "Alan", 2),
new("4", "Audit log", "Katherine", 8),
new("5", "Faster search", "Ada", 5),
];
private string? _saved;
private void Save()
{
// A menu move posts too; only the Save button saves.
if (Order?.Move is not null)
{
return;
}
if (Order is not null)
{
_tasks = Order.Arrange(_tasks, t => t.Id);
}
_saved = $"Saved: {string.Join(", ", _tasks.Select(t => t.Title))}.";
}
}Static: Drag a handle, or use its menu or Alt+arrow keys. Rows move on the page and the move is announced. Save order posts the new order.
On static pages, give the grid an OrderName inside a form and pass the posted ListOrder back as Order; Order.Arrange puts your rows in the posted order. On interactive pages, bind @bind-Items to keep the new order.
Warning:
Don't sort or filter a reorderable grid