Drag and Drop in ASP.NET MVC Tree Grid Component
21 Dec 202312 minutes to read
The TreeGrid rows can be reordered, dropped to another TreeGrid or custom control by enabling the allowRowDragAndDrop
to true.
Drag and drop within Tree Grid
The TreeGrid row drag and drop allows you to drag and drop TreeGrid rows on the same TreeGrid using drag icon. To enable row drag and drop, set the allowRowDragAndDrop
to true. It provides the way to drop the row above, below or child to the target row with respective to the target row position.
@(Html.EJS().TreeGrid("TreeGrid")
.AllowRowDragAndDrop(true)
.Height(275)
.DataSource((IEnumerable<object>
)ViewBag.datasource)
.SelectionSettings(selection => selection.Type(Syncfusion.EJ2.TreeGrid.SelectionType.Multiple))
.Columns(col =>
{
col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(TextAlign.Right).Add();
col.Field("TaskName").HeaderText("Task Name").Width(210).Add();
col.Field("StartDate").HeaderText("Start Date").TextAlign(TextAlign.Right).Format("yMd").Width(210).Add();
col.Field("Duration").HeaderText("Duration").TextAlign(TextAlign.Right).Width(190).Add();
})
.ChildMapping("Children").TreeColumnIndex(1).Render())
public IActionResult Index()
{
var tree = TreeData.GetDefaultData();
ViewBag.datasource = tree;
return View();
}
NOTE
- Selection feature must be enabled for row drag and drop.
* For multiple row selection, thetype
property must be set tomultiple
.
* TheisPrimaryKey
property is necessary to perform row drag and drop operation.
Drag and drop to another Tree Grid
To drag and drop between two TreeGrid, enable the allowRowDragAndDrop
property and specify the target TreeGrid ID in targetID
property of rowDropSettings.
@(Html.EJS().TreeGrid("TreeGrid")
.AllowRowDragAndDrop(true)
.RowDropSettings(new Syncfusion.EJ2.TreeGrid.TreeGridRowDropSettings() { TargetID = "DestTree" })
.Height(275)
.DataSource((IEnumerable<object>
)ViewBag.datasource)
.SelectionSettings(selection => selection.Type(Syncfusion.EJ2.TreeGrid.SelectionType.Multiple))
.Columns(col =>
{
col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(TextAlign.Right).Add();
col.Field("TaskName").HeaderText("Task Name").Width(210).Add();
col.Field("StartDate").HeaderText("Start Date").TextAlign(TextAlign.Right).Format("yMd").Width(210).Add();
col.Field("Duration").HeaderText("Duration").TextAlign(TextAlign.Right).Width(190).Add();
})
.ChildMapping("Children").TreeColumnIndex(1).Render())
@(Html.EJS().TreeGrid("DestTree")
.AllowRowDragAndDrop(true)
.RowDropSettings(drop => drop.TargetID("#TreeGrid"))
.Height(275)
.SelectionSettings(selection => selection.Type(Syncfusion.EJ2.TreeGrid.SelectionType.Multiple))
.Columns(col =>
{
col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(TextAlign.Right).Add();
col.Field("TaskName").HeaderText("Task Name").Width(210).Add();
col.Field("StartDate").HeaderText("Start Date").TextAlign(TextAlign.Right).Format("yMd").Width(210).Add();
col.Field("Duration").HeaderText("Duration").TextAlign(TextAlign.Right).Width(190).Add();
})
.ChildMapping("Children").TreeColumnIndex(1).Render())
public IActionResult Index()
{
var tree = TreeData.GetDefaultData();
ViewBag.datasource = tree;
return View();
}
Drag and drop events
The following events are triggered while drag and drop the treegrid rows.
RowDragStartHelper
- Triggers when click the drag icon or treegrid row and this event is used to customize the drag element based on user criteria.
RowDragStart
-Triggers when starts to drag the treegrid row.
RowDrag
- Triggers while dragging the treegrid row.
RowDrop
- Triggers when a drag element is dropped on the target element.
Prevent reordering a row as child to another row
You can prevent the default behavior of dropping rows as children to the target by setting the cancel
property to true
in rowDrop event argument. You can also change the drop position after cancelling using reorderRows
method.
In the below example drop action is cancelled and dropped above to target row.
@model List<TreeGridSample.Controllers.TreeGridItems>
@(Html.EJS().TreeGrid("TreeGrid")
.AllowRowDragAndDrop(true)
.Height(275)
.RowDrop("rowDrop")
.DataSource((IEnumerable<object>)Model)
.Columns(col =>
{
col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(TextAlign.Right).Add();
col.Field("TaskName").HeaderText("Task Name").Width(210).Add();
col.Field("StartDate").HeaderText("Start Date").TextAlign(TextAlign.Right).Format("yMd").Width(210).Add();
col.Field("Duration").HeaderText("Duration").TextAlign(TextAlign.Right).Width(190).Add();
})
.ChildMapping("Children").TreeColumnIndex(1).Render())
<script>
function rowDrop(args) {
if (args.dropPosition == 'middleSegment') {
var treeGridObj = document.getElementById('TreeGrid').ej2_instances[0];
args.cancel = true;
treeGridObj.reorderRows([args.fromIndex], args.dropIndex, 'above');
}
}
</script>
public class HomeController : Controller
{
public ActionResult Index()
{
return View(TreeGridItems.GetTreeData());
}
}
public class TreeGridItems
{
public TreeGridItems() { }
public int TaskId { get; set; }
public string TaskName { get; set; }
public DateTime StartDate { get; set; }
public int Duration { get; set; }
public List<TreeGridItems> Children { get; set; }
public static List<TreeGridItems> GetTreeData()
{
List<TreeGridItems> BusinessObjectCollection = new List<TreeGridItems>();
TreeGridItems Record1 = null;
Record1 = new TreeGridItems()
{
TaskId = 1,
TaskName = "Planning",
StartDate = new DateTime(2016, 06, 07),
Duration = 5,
Children = new List<TreeGridItems>(),
};
TreeGridItems Child1 = new TreeGridItems()
{
TaskId = 2,
TaskName = "Plan timeline",
StartDate = new DateTime(2016, 06, 07),
Duration = 5
};
TreeGridItems Child2 = new TreeGridItems()
{
TaskId = 3,
TaskName = "Plan budget",
StartDate = new DateTime(2016, 06, 07),
Duration = 5
};
TreeGridItems Child3 = new TreeGridItems()
{
TaskId = 4,
TaskName = "Allocate resources",
StartDate = new DateTime(2016, 06, 07),
Duration = 5
};
Record1.Children.Add(Child1);
Record1.Children.Add(Child2);
Record1.Children.Add(Child3);
TreeGridItems Record2 = new TreeGridItems()
{
TaskId = 6,
TaskName = "Design",
StartDate = new DateTime(2021, 08, 25),
Duration = 3,
Children = new List<TreeGridItems>()
};
TreeGridItems Child5 = new TreeGridItems()
{
TaskId = 7,
TaskName = "Software Specification",
StartDate = new DateTime(2021, 08, 25),
Duration = 3
};
TreeGridItems Child6 = new TreeGridItems()
{
TaskId = 8,
TaskName = "Develop prototype",
StartDate = new DateTime(2021, 08, 25),
Duration = 3
};
TreeGridItems Child7 = new TreeGridItems()
{
TaskId = 9,
TaskName = "Get approval from customer",
StartDate = new DateTime(2024, 06, 27),
Duration = 2
};
Record2.Children.Add(Child5);
Record2.Children.Add(Child6);
Record2.Children.Add(Child7);
BusinessObjectCollection.Add(Record1);
BusinessObjectCollection.Add(Record2);
return BusinessObjectCollection;
}
}
NOTE
You can refer to our
ASP.NET MVC Tree Grid
feature tour page for its groundbreaking feature representations. You can also explore ourASP.NET MVC Tree Grid example
to knows how to present and manipulate data.