|
<SfTreeGrid @ref="TreeGrid" TValue="TreeData.BusinessObject"
IdMapping="TaskId" ParentIdMapping="ParentId" TreeColumnIndex="1"
AllowPaging="true" HasChildMapping="isParent" AllowRowDragAndDrop="true">
<SfDataManager Url="/api/Values" Adaptor="Adaptors.UrlAdaptor"></SfDataManager>
<TreeGridEvents TValue="TreeData.BusinessObject" RowDropped="RowDrop"></TreeGridEvents>
<TreeGridSelectionSettings Type="Syncfusion.Blazor.Grids.SelectionType.Multiple"></TreeGridSelectionSettings>
<TreeGridColumns>
<TreeGridColumn Field="TaskId" HeaderText="Task ID" IsPrimaryKey="true" Width="80"
TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>
-------
</TreeGridColumns>
</SfTreeGrid>
@code{
SfTreeGrid<TreeData.BusinessObject> TreeGrid;
void RowDrop(Syncfusion.Blazor.Grids.RowDragEventArgs<TreeData.BusinessObject> args)
{
var position = args.Target.ID; // drop position
var dropIndex = Convert.ToInt32(args.DropIndex); // drop index
var targetData = this.TreeGrid.GetCurrentViewRecords()[dropIndex]; // drop index data
var targetDataID = targetData.TaskId; // drop index data's TaskId
var index = Convert.ToInt32(args.DropIndex); // drop index
//args.Cancel = true;
if (position == " e-droptop" || position == " e-dropbottom") // if a record is dropped below/above another record
{
if (args.Data[0].ParentId != null) // if dragged record has parent
{
var childCount = 0;
int parent1 = (int)args.Data[0].ParentId;
childCount += FindChildRecords(parent1); // finding the number of child for dragged record's parent
---------
}
var data = TreeData.GetSelfDataSource().Where(ds => ds.TaskId == args.Data[0].TaskId).FirstOrDefault();
//removing dragged record from list
TreeData.GetSelfDataSource().Remove(TreeData.GetSelfDataSource().Where(ds => ds.TaskId == args.Data[0].TaskId).FirstOrDefault());
data.ParentId = targetData.ParentId; //setting the dropIndex data's parent as parent of dragged record
if (position == " e-dropbottom") // if a record is dropped below another record
{
index = index + 1;
TreeData.BusinessObjectCollection.Insert(index, data);
}
else if (position == " e-droptop") // if a record is dropped above another record
{
index = index + FindChildRecords(targetDataID); // find the target record's children count to find drop index
TreeData.BusinessObjectCollection.Insert(index, data);
}
}
else if (position == " e-dropchild") // if a record is dropped as child to another record(middle segment)
{
var data = TreeData.GetSelfDataSource().Where(ds => ds.TaskId == args.Data[0].TaskId).FirstOrDefault();
var dropData = TreeData.GetSelfDataSource().Where(ds => ds.TaskId == targetDataID).FirstOrDefault();
// removing dragged record from list
TreeData.GetSelfDataSource().Remove(TreeData.GetSelfDataSource().Where(ds => ds.TaskId == args.Data[0].TaskId).FirstOrDefault());
dropData.isParent = true; //set isParent property(hasChildMapping) of target record to false
data.ParentId = targetDataID; //setting the dropIndex data's ID as parent of dragged record
index = index + 1;
TreeData.BusinessObjectCollection.Insert(index, data);
}
}
}
|
Hi,
is there a simpler solution using a CustomAdapter yet? Why is the Update method of the CustomAdapter not automatically called? Do I really need to check the drop positionmyself?
Best,
Gernot
Regarding your query, when using Row Drag and Drop with a custom adapter, you will need to handle the drag and drop operation. Based on the dragged record and the drop position, you will need to remove and add the record at the dropped position.
As for the Update method of the CustomAdapter, it is not automatically called during the drag and drop operation. So you will need to manually handle the drop position.
Please refer to the below code snippet,
|
<SfTreeGrid @ref="treeGrid" TValue="TreeData.BusinessObject" HasChildMapping="isParent" IdMapping="TaskId" ParentIdMapping="ParentId" AllowRowDragAndDrop="true" TreeColumnIndex="1" > <SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager> … <TreeGridEvents TValue="TreeData.BusinessObject" RowDropped="Dropping" ></TreeGridEvents> <TreeGridColumns> <TreeGridColumn Field="TaskId" HeaderText="Task ID" IsPrimaryKey="true" Width="80" …. </SfTreeGrid> @code{ ….
protected override void OnInitialized() { TreeGridData = TreeData.GetSelfDataSource().ToList(); }
private void Dropping(Syncfusion.Blazor.Grids.RowDragEventArgs<TreeData.BusinessObject> args) { var position = args.Target.ID; // drop position var dropIndex = Convert.ToInt32(args.DropIndex); // drop index var targetDataID = treeGrid.GetCurrentViewRecords()[dropIndex].TaskId; // drop index data's TaskId var targetData = TreeData.GetSelfDataSource().Where(ds => ds.TaskId == targetDataID).FirstOrDefault(); var index = Convert.ToInt32(args.DropIndex); // drop index if (position == " e-droptop" || position == " e-dropbottom") // if a record is dropped below/above another record { if (args.Data[0].ParentId != null) // if dragged record has parent { var childCount = 0; int parent1 = (int)args.Data[0].ParentId; childCount += FindChildRecords(parent1); // finding the number of child for dragged record's parent if (childCount == 1) // if the dragged record is the only child for a particular record { var i = 0; for (; i < TreeData.GetSelfDataSource().Count; i++) { if (TreeData.GetSelfDataSource()[i].TaskId == parent1) { //set isParent of dragged record's parent to false TreeData.GetSelfDataSource()[i].isParent = null; break; } if (TreeData.GetSelfDataSource()[i].TaskId == args.Data[0].TaskId) { //set parentItem of dragged record to null TreeData.GetSelfDataSource()[i].ParentId = null; break; }
} } } var data = TreeData.GetSelfDataSource().Where(ds => ds.TaskId == args.Data[0].TaskId).FirstOrDefault(); //removing dragged record from list TreeData.GetSelfDataSource().Remove(TreeData.GetSelfDataSource().Where(ds => ds.TaskId == args.Data[0].TaskId).FirstOrDefault()); data.ParentId = targetData.ParentId; //setting the dropIndex data's parent as parent of dragged record
if (position == " e-dropbottom") // if a record is dropped below another record { index = TreeData.BusinessObjectCollection.IndexOf(targetData); index = index + 1; TreeData.BusinessObjectCollection.Insert(index, data);
} else if (position == " e-droptop") // if a record is dropped above another record { index = TreeData.BusinessObjectCollection.IndexOf(targetData); index = index + FindChildRecords(targetDataID); // find the target record's children count to find drop index TreeData.BusinessObjectCollection.Insert(index, data);
} }
else if (position == " e-dropchild") // if a record is dropped as child to another record(middle segment) { var data = TreeData.GetSelfDataSource().Where(ds => ds.TaskId == args.Data[0].TaskId).FirstOrDefault(); var dropData = TreeData.GetSelfDataSource().Where(ds => ds.TaskId == targetDataID).FirstOrDefault(); // removing dragged record from list TreeData.GetSelfDataSource().Remove(TreeData.GetSelfDataSource().Where(ds => ds.TaskId == args.Data[0].TaskId).FirstOrDefault()); dropData.isParent = true; //set isParent property(hasChildMapping) of target record to false data.ParentId = targetDataID; //setting the dropIndex data's ID as parent of dragged record index = index + 1; TreeData.BusinessObjectCollection.Insert(index, data);
} TreeGridData = TreeData.BusinessObjectCollection.ToList(); } public int FindChildRecords(int? id) { var count = 0; for (var i = 0; i < TreeData.GetSelfDataSource().Count; i++) { if (TreeData.GetSelfDataSource()[i].ParentId == id) { count++; count += FindChildRecords(TreeData.GetSelfDataSource()[i].TaskId); } } return count; }
…..
|
Please refer to the attached sample linked below for an example of using custom adapter with Row Drag and Drop:
Kindly get back to us for further assistance.
Regards,
Pon selva