- Home
- Forum
- Xamarin.Forms
- Saving Re-ordered items
Saving Re-ordered items
Hello,
I have a list of data objects loaded into sfListview. Each object has an OrderIndex property.
When I load the sfListView, I use the SortDescriptor and sort the displayed list on their OrderIndex property.
When the user re-orders an item, I can see the oldIndex and newIndex on the ItemDraggingEventArgs.
Once the user drops the item, I want to re-save all the objects with their new displayed index (into their OrderIndex property).
How do I get the list or collection of displayed items AFTER the drag/drop? AND in the order in which their are newly displayed, NOT their original order.
ie: with the new indexes in place, I can now resave all/each item with it's new index.
Edit: Work around. The newly arranged list of items is not available until the Dragging event has been handled.
If my users navigate away from that page, the arranged list is lost so save list before navigating away.
I do this in a save event...
Call list.SelectAll();
Get the list.SelectedItems;
Loop through selected items in foreach loop, setting the OrderIndex property to current loop index.
Save each selected item.
SIGN IN To post a reply.
5 Replies
RS
Rawoof Sharief Muthuja Sherif
Syncfusion Team
March 27, 2018 05:28 PM UTC
Hi Lindsay,
We would like to let you know that you can achieve your requirement using Itemdragging event in SfListView. When the dragging has ended, the e.Action 'Stop' will be executed. You need move the underlying collection based on the new index and old index obtained from item dragging event args. You need time delay to re-arrange the OrderIndex for all the items in the View. Please refer the below code example.
Code Example[C#]:
We would like to let you know that you can achieve your requirement using Itemdragging event in SfListView. When the dragging has ended, the e.Action 'Stop' will be executed. You need move the underlying collection based on the new index and old index obtained from item dragging event args. You need time delay to re-arrange the OrderIndex for all the items in the View. Please refer the below code example.
Code Example[C#]:
|
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
listView.ItemDragging += ListView_ItemDraggingAsync;
}
private async void ListView_ItemDraggingAsync(object sender, ItemDraggingEventArgs e)
{
if (e.Action == DragAction.Drop)
{
if (e.OldIndex < e.NewIndex)
{
await Task.Delay(100);
ChangeData(e);
}
}
}
private void ChangeData(ItemDraggingEventArgs e)
{
ViewModel.ToDoList.MoveTo(e.OldIndex, e.NewIndex);
for (int i = 0; i < ViewModel.ToDoList.Count; i++)
{
(ViewModel.ToDoList[i] as ToDoItem).OrderIndex = i;
}
}
} |
For your reference, we have attached the sample and you can download it from the below link.
Sample link: http://www.syncfusion.com/downloads/support/forum/136647/ze/ReorderingSample-2094191303
Please let us know if your require further assistance.
Regards,
Rawoof M.
LM
Lindsay Miles
March 27, 2018 05:49 PM UTC
thanks for the tip, I will try implement that tonight and see if it suits my needs.
MK
Muthu Kumaran Gnanavinayagam
Syncfusion Team
March 28, 2018 04:39 AM UTC
Hi Lindsay,
In our sample, we have provided solution to reorder the item index while dragging item from top to bottom of the list. So if you need to reorder the index when dropped an item from bottom to top of the list, you can ignore the condition to check whether the NewIndex is greater than OldIndex and use the code example as like below.
Code Example[C#]:
|
private async void ListView_ItemDraggingAsync(object sender, ItemDraggingEventArgs e)
{
if (e.Action == DragAction.Drop)
{
await Task.Delay(100);
ChangeData(e);
}
}
private void ChangeData(ItemDraggingEventArgs e)
{
ViewModel.ToDoList.MoveTo(e.OldIndex, e.NewIndex);
for (int i = 0; i < ViewModel.ToDoList.Count; i++)
{
(ViewModel.ToDoList[i] as ToDoItem).OrderIndex = i;
}
} |
Please let us know if you require further assistance.
Regards,
G.Muthu kumaran.
LM
Lindsay Miles
March 28, 2018 01:45 PM UTC
Sorry, but what's the pint in checking e.OldIndex < e.NewIndex if there's no difference in how the ChangeData() method handles the changes?
Thx
MK
Muthu Kumaran Gnanavinayagam
Syncfusion Team
March 29, 2018 06:11 AM UTC
Hi Lindsay,
As we have altered the OrderIndex for the entire source collection when dropped an item in a new index, it is not needed to check for the condition whether the NewIndex is greater than the OldIndex. We have mentioned regarding this in our previous update.
Please let us know if you require further assistance.
Regards,
G.Muthu kumaran.
SIGN IN To post a reply.
- 5 Replies
- 3 Participants
-
LM Lindsay Miles
- Mar 25, 2018 04:31 PM UTC
- Mar 29, 2018 06:11 AM UTC