Example of Drag and Drop in ASP.NET MVC Tabs Control
This example illustrates how to reorder tabs and add tabs from an external source(list of components) by drag and drop. Here, you can drag and drop the items from TreeView into Tab.
| Order ID | Customer ID | Freight | Order Date |
|---|---|---|---|
| 10248 | VINET | $32.38 | 7/4/1996 |
| 10249 | TOMSP | $11.61 | 7/5/1996 |
| 10250 | HANAR | $65.83 | 7/8/1996 |
| 10251 | VICTE | $41.34 | 7/8/1996 |
| 10252 | SUPRD | $51.30 | 7/9/1996 |
@Html.UiTabs(new UiTabsOptions
{
Id = "ej2Tab",
AllowDragAndDrop = true,
Items = new List<UiTabItem>
{
new UiTabItem { Header = "DataGrid", Template = @<table class="drag-grid"><thead><tr><th>Order ID</th><th>Customer ID</th><th>Freight</th><th>Order Date</th></tr></thead><tbody>@foreach (var order in (List<NavigationDemoController.TabOrder>)ViewData["gridData"]) { <tr><td>@order.OrderID</td><td>@order.CustomerID</td><td>@order.Freight.ToString("C")</td><td>@order.OrderDate.ToString("d")</td></tr> }</tbody></table> },
new UiTabItem { Header = "Chart", Template = @<div class="drag-chart">@foreach (var point in (List<NavigationDemoController.TabSales>)ViewData["chartData"]) { <div class="drag-bar" title="@point.Month: @point.Sales" style="height: @(point.Sales * 2)px"><span>@point.Month</span></div> }</div> },
new UiTabItem { Header = "Schedule", Content = "The Schedule shows appointments by day, week and month." }
}
})
<style>
.e-sample-resize-container { max-width: 760px; margin: 0 auto; padding-top: 20px; }
.drag-grid { width: 100%; border-collapse: collapse; }
.drag-grid th, .drag-grid td { padding: 6px 8px; border-bottom: 1px solid #ddd; text-align: left; }
.drag-chart { display: flex; align-items: flex-end; gap: 14px; height: 220px; padding-top: 10px; }
.drag-bar { position: relative; width: 36px; background: var(--ui-accent, #0f6cbd); border-radius: 3px 3px 0 0; }
.drag-bar span { position: absolute; left: 0; right: 0; bottom: -20px; font-size: 11px; text-align: center; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div id="TabContainer">
<div class="content-wrapper control-section">
<div class="e-sample-resize-container">
@Html.UiTabs(new UiTabsOptions
{
Id = "ej2Tab",
AllowDragAndDrop = true,
Items = new List<UiTabItem>
{
new UiTabItem { Header = "DataGrid", Template = @<table class="drag-grid"><thead><tr><th>Order ID</th><th>Customer ID</th><th>Freight</th><th>Order Date</th></tr></thead><tbody>@foreach (var order in (List<NavigationDemoController.TabOrder>)ViewData["gridData"]) { <tr><td>@order.OrderID</td><td>@order.CustomerID</td><td>@order.Freight.ToString("C")</td><td>@order.OrderDate.ToString("d")</td></tr> }</tbody></table> },
new UiTabItem { Header = "Chart", Template = @<div class="drag-chart">@foreach (var point in (List<NavigationDemoController.TabSales>)ViewData["chartData"]) { <div class="drag-bar" title="@point.Month: @point.Sales" style="height: @(point.Sales * 2)px"><span>@point.Month</span></div> }</div> },
new UiTabItem { Header = "Schedule", Content = "The Schedule shows appointments by day, week and month." }
}
})
</div>
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TabController : Controller
{
// The page is Views/Tab/DragAndDrop.cshtml; it draws what ViewData holds.
public ActionResult DragAndDrop()
{
ViewData["gridData"] = new List<TabOrder>
{
new TabOrder { OrderID = 10248, CustomerID = "VINET", Freight = 32.38, OrderDate = new DateTime(1996, 7, 4) },
new TabOrder { OrderID = 10249, CustomerID = "TOMSP", Freight = 11.61, OrderDate = new DateTime(1996, 7, 5) },
new TabOrder { OrderID = 10250, CustomerID = "HANAR", Freight = 65.83, OrderDate = new DateTime(1996, 7, 8) },
new TabOrder { OrderID = 10251, CustomerID = "VICTE", Freight = 41.34, OrderDate = new DateTime(1996, 7, 8) },
new TabOrder { OrderID = 10252, CustomerID = "SUPRD", Freight = 51.3, OrderDate = new DateTime(1996, 7, 9) }
};
ViewData["chartData"] = new List<TabSales>
{
new TabSales { Month = "Jan", Sales = 35 },
new TabSales { Month = "Feb", Sales = 28 },
new TabSales { Month = "Mar", Sales = 34 },
new TabSales { Month = "Apr", Sales = 32 },
new TabSales { Month = "May", Sales = 40 },
new TabSales { Month = "Jun", Sales = 32 }
};
return View();
}
/// <summary>A row of the Drag and Drop sample's grid.</summary>
public class TabOrder
{
public int OrderID { get; set; }
public string CustomerID { get; set; }
public double Freight { get; set; }
public DateTime OrderDate { get; set; }
}
/// <summary>A point of the Drag and Drop sample's chart.</summary>
public class TabSales
{
public string Month { get; set; }
public int Sales { get; set; }
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
In this example, the AllowDragAndDrop property is used to enable drag and drop and the DragArea property is used to define the draggable area.
In this example, the list of components is rendered using the Treeview component. We can drag the item from the treeview component to the tab component by using the NodeDragStop event of the treeview component and add the same item with the help of the addTab public method of Tab and remove this item from the treeview by using the removeNodes method.
In the same way, we can drag the tab item within the tab component and also add the tab item to the treeview component. Here, we can drop the tab item in the treeview component by using the Dragged event of the tab component. In this case, we can remove the dropped item from tab with the help of the removeTab public method and add the item to the treeview in its addNodes public method.