-
Notifications
You must be signed in to change notification settings - Fork 31
Description
Hi!
First off, nice work! I've tried to implement it within my BlazorForms project, which allows the user to built rich interactive forms using drag & drop and blazor.
My issue is that I cannot figure out how to work with dynamic lists. For example I have a class which hols a list of another class and this one is holding another list of another class again. Basically this:
public class Form
{
public List<FormRow> Rows { get; set; } = [];
}
public class FormRow
{
public List<FormElement> Elements { get; set; } = [];
}
public class FormElement
{
public string Name { get; set; } = string.Empty;
public FormElement(string name)
{
Name = name;
}
}One instanciated model may look like this:
public Form MyForm { get; set; } = new Form
{
Rows =
[
new FormRow()
{
Elements =
[
new FormElement("Hello World 1"),
new FormElement("Hello World 2"),
new FormElement("Hello World 3"),
new FormElement("Hello World 4"),
]
},
new FormRow()
{
Elements =
[
new FormElement("Hello World 5"),
new FormElement("Hello World 6"),
new FormElement("Hello World 7"),
new FormElement("Hello World 8"),
]
},
]
};Using generics I pass the source list to my sorting function like this:
private void SortList<T>((int oldIndex, int newIndex) indices, List<T> items)
{
// deconstruct the tuple
var (oldIndex, newIndex) = indices;
var itemToMove = items[oldIndex];
items.RemoveAt(oldIndex);
if (newIndex < items.Count)
{
items.Insert(newIndex, itemToMove);
}
else
{
items.Add(itemToMove);
}
StateHasChanged();
}The list itself is implemented like this:
<SortableList Id="@Guid.NewGuid().ToString()" Items="MyForm.Rows" OnUpdate="(indices) => SortList<FormRow>(indices, MyForm.Rows)" Context="row">
<SortableItemTemplate>
<div class="has-border has-background-white has-cursor-pointer">
<p class="is-size-4 p-2 ml-4">
<div class="form-row">
This is a row.
<SortableList Id="@Guid.NewGuid().ToString()" Items="row.Elements" OnUpdate="(indices) => SortList<FormElement>(indices, row.Elements)" Context="element">
<SortableItemTemplate>
<div class="has-border has-background-white has-cursor-pointer">
<div class="form-element">
@element.Name
</div>
</div>
</SortableItemTemplate>
</SortableList>
</div>
</p>
</div>
</SortableItemTemplate>
</SortableList>This works fine for me. I'm able to sort all rows and all elements. However I want to be able to move one FormElement to another FormRow. So basically remove it from source and then add it to the target list.
But there is no way for me to determine the target list. The List of FormRow from the Form class can have unlimited amount of rows.
Do you have any clue how this can be achieved?