|
@using Syncfusion.Blazor.DropDowns
@using Newtonsoft.Json
<div class="col-lg-12 control-section">
<div id="drag-drop-wrapper">
<div class="listbox-control">
<h4>Group A</h4>
<SfListBox @ref="listbox1" ID="listbox1" DataSource="@GroupA" Scope="combined-list" TItem="ListData" AllowDragAndDrop="true" Height="330px" TValue="string[]">
<ListBoxEvents TValue="string[]" Dropped="onDropped"></ListBoxEvents>
<ListBoxFieldSettings Text="Name"></ListBoxFieldSettings>
</SfListBox>
</div>
</div>
</div>
@code{
SfListBox<string[], ListData> listbox1;
public object listboxData = new object();
public int indexPos;
public List<ListData> GroupA = new List<ListData> {
new ListData { Name = "Australia", Code = "AU", Index = 1 },
new ListData { Name = "Bermuda", Code = "BM", Index = 2 },
new ListData { Name = "Canada", Code = "CA", Index = 3 },
new ListData { Name = "Cameroon", Code = "CM", Index = 4 },
new ListData { Name = "Denmark", Code = "DK", Index = 5 },
new ListData { Name = "France", Code = "FR", Index = 6 },
new ListData { Name = "Finland", Code = "FI", Index = 7 },
new ListData { Name = "Germany", Code = "DE", Index = 8 },
new ListData { Name = "Hong Kong", Code = "HK", Index = 9 }
};
public class ListData
{
public string Name { get; set; }
public string Code { get; set; }
public int Index { get; set; }
}
protected async Task onDropped(Syncfusion.Blazor.DropDowns.DragEventArgs args)
{
listboxData = await listbox1.GetDataList();
List<ListData> ds = JsonConvert.DeserializeObject<List<ListData>>(JsonConvert.SerializeObject(listboxData));
List<ListData> lists = JsonConvert.DeserializeObject<List<ListData>>(JsonConvert.SerializeObject(args.Items));
indexPos = ds.FindIndex(a => a.Name == lists[0].Name);
ListData datasource = GroupA.Find(a => a.Name == lists[0].Name);
datasource.Index = indexPos; // you can set the dragged position in this index
}
} |
|
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.DropDowns
@using Newtonsoft.Json
@using Syncfusion.Blazor.Inputs;
@using System.Collections.ObjectModel
@using SortOrder = Syncfusion.Blazor.Lists.SortOrder
@using System.Collections.ObjectModel
<style>
.e-listbox-wrapper:not(.e-list-template) .e-list-item, .e-listbox-wrapper .e-selectall-parent {
height: initial !important;
}
</style>
<div class="col-lg-12 control-section">
<div id="drag-drop-wrapper">
<div class="listbox-control">
<h4>Group A</h4>
<SfButton OnClick="@AddNewCountry" Content="Add New Country"></SfButton>
<SfListBox @ref="listbox1" ID="listbox1" SortOrder="SortOrder.Ascending" DataSource="@GroupA" Scope="combined-list" TItem="ListData" AllowDragAndDrop="true" Height="330px" TValue="string[]">
<ListBoxEvents TValue="string[]" Dropped="onDropped"></ListBoxEvents>
<ListBoxFieldSettings Text="Index"></ListBoxFieldSettings>
<ListBoxTemplates TItem="ListData">
<ItemTemplate>
<div class="d-flex" style="padding: 0.75rem; border-top: 1px solid #eaeaea;">
<div class="d-flex align-items-center" style="width: 100px;">
<span class="btn oi oi-delete " style="cursor: pointer;" @onclick=@(async () => { RemoveItem(context); })></span>
</div>
<div class="d-flex align-items-center flex-grow-1">
<div>@((context as ListData).Name)</div>
<div>Index: @((context as ListData).Index)</div>
</div>
<div style="width: 150px;">
<SfNumericTextBox TValue="int" @bind-Value="@((context as ListData).PeopleCount)" Decimals="0" Min="1" Step="1"></SfNumericTextBox>
</div>
</div>
</ItemTemplate>
</ListBoxTemplates>
</SfListBox>
</div>
</div>
</div>
@code{
SfListBox<string[], ListData> listbox1;
public ObservableCollection<ListData> GroupA { get; set; }
public async Task AddNewCountry(MouseEventArgs args)
{
this.GroupA.Add(new ListData()
{
Code = "CH",
Name = "Switzerland",
PeopleCount = 10
});
}
public void RemoveItem(ListData listData)
{
this.GroupA.Remove(listData);
int index = listData.Index;
this.GroupA.ToList().ForEach((i) =>
{
if (index < i.Index)
{ i.Index = i.Index - 1; }
});
}
public class ListData
{
public string Name { get; set; }
public string Code { get; set; }
public int PeopleCount { get; set; }
public int Index { get; set; }
public static ObservableCollection<ListData> getListData()
{
ObservableCollection<ListData> Data = new ObservableCollection<ListData>();
Data.Add(new ListData { Name = "Canada", Code = "CA", Index = 3, PeopleCount = 8 });
Data.Add(new ListData { Name = "Cameroon", Code = "CM", Index = 4, PeopleCount = 200 });
Data.Add(new ListData { Name = "Denmark", Code = "DK", Index = 5, PeopleCount = 14 });
Data.Add(new ListData { Name = "France", Code = "FR", Index = 6, PeopleCount = 300 });
Data.Add(new ListData { Name = "Australia", Code = "AU", Index = 1, PeopleCount = 3 });
Data.Add(new ListData { Name = "Bermuda", Code = "BM", Index = 2, PeopleCount = 6 });
Data.Add(new ListData { Name = "Finland", Code = "FI", Index = 7, PeopleCount = 466 });
Data.Add(new ListData { Name = "Germany", Code = "DE", Index = 8, PeopleCount = 450 });
Data.Add(new ListData { Name = "Hong Kong", Code = "HK", Index = 9, PeopleCount = 200 });
return Data;
}
}
protected override void OnInitialized()
{
this.GroupA = ListData.getListData();
}
protected async Task onDropped(Syncfusion.Blazor.DropDowns.DragEventArgs args)
{
var listboxData = await listbox1.GetDataList();
ObservableCollection<ListData> ds = JsonConvert.DeserializeObject<ObservableCollection<ListData>>(JsonConvert.SerializeObject(listboxData));
ObservableCollection<ListData> lists = JsonConvert.DeserializeObject<ObservableCollection<ListData>>(JsonConvert.SerializeObject(args.Items));
var indexPos = ds.IndexOf(ds.Where(X => X.Name == lists[0].Name).FirstOrDefault());
ListData datasource = GroupA.Where(i => i.Name == lists[0].Name).First();
datasource.Index = indexPos; // you can set the dragged position in this index
}
} |
|
protected async Task onDropped(Syncfusion.Blazor.DropDowns.DragEventArgs args)
{
var listboxData = await listbox1.GetDataList();
ObservableCollection<ListData> ds = JsonConvert.DeserializeObject<ObservableCollection<ListData>>(JsonConvert.SerializeObject(listboxData));
ObservableCollection<ListData> lists = JsonConvert.DeserializeObject<ObservableCollection<ListData>>(JsonConvert.SerializeObject(args.Items));
var prevPos = lists[0].Index - 1;
var indexPos = ds.IndexOf(ds.Where(X => X.Name == lists[0].Name).FirstOrDefault());
ListData datasource = GroupA.Where(i => i.Name == lists[0].Name).First();
this.GroupA.ToList().ForEach((i) =>
{
if (prevPos > indexPos && i.Index >= indexPos && i.Index <= prevPos && i.Name != lists[0].Name) // dragged from bottom to top
{ i.Index = i.Index + 1; }
else if(prevPos < indexPos && i.Index >= prevPos && i.Index <= indexPos && i.Name != lists[0].Name) // dragged from top to bottom
{
i.Index = i.Index - 1;
}
});
datasource.Index = indexPos + 1; // you can set the dragged position in this index
} |
Hello Sergio,