I’m trying to implement drag-and-drop functionality in a Blazor application using MudBlazor. I aim to have a single drop zone containing a table (MudTable) displaying a student list. I’ve tried several approaches but need help getting the drag-and-drop to work correctly. Here are the details of my current implementation and the issues I’ve encountered.
Current Implementation
I started with the following code:
@page "/main"
@using MudBlazor
<MudPaper Class="ma-4 flex-grow-1">
<MudDropContainer T="Student" Items="_students" ItemDropped="ItemUpdated" Class="d-flex flex-column mud-height-full">
<ChildContent>
<MudDropZone T="Student" Identifier="singleDropZone" Class="flex-grow-1" AllowReorder="true">
<MudTable Items="_students">
<HeaderContent>
<MudTh>Name</MudTh>
<MudTh>Age</MudTh>
<MudTh>Degree</MudTh>
<MudTh>College</MudTh>
</HeaderContent>
<RowTemplate>
<MudTr>
<MudTd DataLabel="Name">@context.Name</MudTd>
<MudTd DataLabel="Age">@context.Age</MudTd>
<MudTd DataLabel="Degree">@context.Degree</MudTd>
<MudTd DataLabel="College">@context.College</MudTd>
</MudTr>
</RowTemplate>
</MudTable>
</MudDropZone>
</ChildContent>
<ItemRenderer>
<MudListItem>
<MudTextField Value="@context.Name" Label="Name" ReadOnly="true" />
<MudTextField Value="@context.Age.ToString()" Label="Age" ReadOnly="true" />
<MudTextField Value="@context.Degree" Label="Degree" ReadOnly="true" />
<MudTextField Value="@context.College" Label="College" ReadOnly="true" />
</MudListItem>
</ItemRenderer>
</MudDropContainer>
</MudPaper>
@code {
private void ItemUpdated(MudItemDropInfo<Student> dropItem)
{
// Remove the item from the old position
_students.Remove(dropItem.Item);
// Insert the item at the new position
_students.Insert(dropItem.Index, dropItem.Item);
}
private List<Student> _students = new()
{
new Student { Name = "John Doe", Age = 20, Degree = "B.Sc", College = "College A" },
new Student { Name = "Jane Smith", Age = 22, Degree = "B.A", College = "College B" },
new Student { Name = "Alice Johnson", Age = 21, Degree = "B.Com", College = "College C" },
new Student { Name = "Bob Brown", Age = 23, Degree = "B.Tech", College = "College D" },
new Student { Name = "Charlie Green", Age = 24, Degree = "B.Arch", College = "College E" }
};
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Degree { get; set; }
public string College { get; set; }
}
}
Issues Encountered
The MudItemDropInfo does not contain an Index property, which caused compilation errors.
I’ve tried other methods of handling the reordering, but they haven’t worked correctly.
Alternative Attempts
Attempt 1
I removed the Index property handling and tried to reorder the list without it:
private void ItemUpdated(MudItemDropInfo<Student> dropItem)
{
// Attempted to reorder without index
var item = dropItem.Item;
_students.Remove(item);
// Logic to insert at new position needed
}
Attempt 2
I wrapped the table rows in a drop zone, but drag-and-drop still didn’t work as expected:
<MudDropZone T="Student" Identifier="singleDropZone" Class="flex-grow-1" AllowReorder="true">
<MudTable Items="_students">
<RowTemplate>
<MudTr>
<MudTd DataLabel="Name">@context.Name</MudTd>
<MudTd DataLabel="Age">@context.Age</MudTd>
<MudTd DataLabel="Degree">@context.Degree</MudTd>
<MudTd DataLabel="College">@context.College</MudTd>
</MudTr>
</RowTemplate>
</MudTable>
</MudDropZone>
Requirements
A single drop zone containing a table.
Ability to drag and drop rows within the table to reorder them.
Question
How can I correctly implement drag-and-drop functionality for a table using MudBlazor? Any guidance or working examples would be greatly appreciated!