Example of Multi Sorting in ASP.NET MVC Tree Grid Control
Sorting feature enables us to order the data in a particular direction. It can be enabled by setting the AllowSorting as true.
Properties
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "Sorting",
OnActionComplete = "actionComplete",
AllowSorting = true,
AllowMultiSorting = true,
Data = ViewData["datasource"],
SortColumns = new List<UiGridSortColumn> { new UiGridSortColumn { Field = "category", Direction = UiSortDirection.Ascending }, new UiGridSortColumn { Field = "orderName", Direction = UiSortDirection.Descending } },
ChildMapping = "subTasks",
Height = "400px",
TreeColumnIndex = 0,
AllowPaging = true,
PageSize = 12,
Selection = UiGridSelection.Single,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "orderName", HeaderText = "Order Name" },
new UiGridColumn { Field = "category", HeaderText = "Category" },
new UiGridColumn { Field = "orderDate", HeaderText = "Order Date", Type = UiColumnType.Date, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "units", HeaderText = "Units", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right }
}
})
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "Sorting",
OnActionComplete = "actionComplete",
AllowSorting = true,
AllowMultiSorting = true,
Data = ViewData["datasource"],
SortColumns = new List<UiGridSortColumn> { new UiGridSortColumn { Field = "category", Direction = UiSortDirection.Ascending }, new UiGridSortColumn { Field = "orderName", Direction = UiSortDirection.Descending } },
ChildMapping = "subTasks",
Height = "400px",
TreeColumnIndex = 0,
AllowPaging = true,
PageSize = 12,
Selection = UiGridSelection.Single,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "orderName", HeaderText = "Order Name" },
new UiGridColumn { Field = "category", HeaderText = "Category" },
new UiGridColumn { Field = "orderDate", HeaderText = "Order Date", Type = UiColumnType.Date, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "units", HeaderText = "Units", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="orderName">Order Name</label><input type="checkbox" id="orderName" checked /></div>
<div class="demo-property-row"><label for="category">Category</label><input type="checkbox" id="category" checked /></div>
<div class="demo-property-row"><label for="orderDate">Order Date</label><input type="checkbox" id="orderDate" /></div>
<div class="demo-property-row"><label for="units">Units</label><input type="checkbox" id="units" /></div>
</div>
<script>
// Syncfusion: a ticked box sorts that column ascending, added to the others; an unticked box removes its sort.
['orderName', 'category', 'orderDate', 'units'].forEach(function (field) {
document.getElementById(field).addEventListener('change', function (e) {
var tree = window.Lumen.get('Sorting');
if (e.target.checked) { tree.sortColumn(field, 'Ascending', true); } else { tree.removeSortColumn(field); }
});
});
// After a sort (a header click too) the boxes show which columns are sorted.
function actionComplete(args) {
if (args.requestType === 'sorting') {
var sorted = args.sortedColumns.map(function (s) { return s.field; });
['orderName', 'category', 'orderDate', 'units'].forEach(function (field) {
document.getElementById(field).checked = sorted.indexOf(field) >= 0;
});
}
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TreeGridController : Controller
{
// The page is Views/TreeGrid/MultiSorting.cshtml; it draws what ViewData holds.
public ActionResult MultiSorting()
{
var treeData = TreeDataFormat.GetDataFormat();
ViewData["datasource"] = treeData;
return View();
}
public class TreeDataFormat
{
public TreeDataFormat() { }
public string orderID { get; set; }
public string orderName { get; set; }
public DateTime orderDate { get; set; }
public DateTime shippedDate { get; set; }
public int units { get; set; }
public double price { get; set; }
public int unitPrice { get; set; }
public string category { get; set; }
public List<TreeDataFormat> subTasks { get; set; }
public static List<TreeDataFormat> GetDataFormat()
{
List<TreeDataFormat> data = new List<TreeDataFormat>()
{
new TreeDataFormat()
{
orderID= "1",
orderName= "Order 1",
orderDate= new DateTime(2017, 03, 02),
shippedDate= new DateTime(2019, 10, 09),
units= 1395,
unitPrice= 47,
price=133.66,
category= "Seafood",
subTasks= new List<TreeDataFormat>() {
new TreeDataFormat() { orderID= "1.1", orderName= "Mackerel", category= "Frozen seafood", units= 23,
orderDate= new DateTime(2017, 03, 02), shippedDate= new DateTime(2019, 05, 13), unitPrice= 12, price= 28.20 },
new TreeDataFormat() { orderID= "1.2", orderName= "Mackerel", category= "Frozen seafood", units= 23,
orderDate= new DateTime(2017, 03, 05), shippedDate= new DateTime(2019, 05, 22), unitPrice= 11, price= 25.92 },
new TreeDataFormat() { orderID= "1.3", orderName= "Mackerel", category= "Frozen seafood", units= 23,
orderDate= new DateTime(2017, 03, 10), shippedDate= new DateTime(2019, 10, 14), unitPrice= 8, price= 52.68 },
new TreeDataFormat() { orderID= "1.4", orderName= "Mackerel", category= "Edible", units= 23,
orderDate= new DateTime(2017, 03, 08), shippedDate= new DateTime(2019, 08, 03), unitPrice= 9, price= 11.25 },
new TreeDataFormat() { orderID= "1.5", orderName= "Mackerel", category= "Edible", units= 23,
orderDate= new DateTime(2017, 03, 09), shippedDate= new DateTime(2019, 03, 09), unitPrice= 7, price= 15.61 }
}
},
new TreeDataFormat()
{
orderID= "2",
orderName= "Order 2",
orderDate= new DateTime(2017, 03, 05),
shippedDate= new DateTime(2019, 05, 03),
units= 1944,
unitPrice= 58,
price=212.33,
category= "Seafood",
subTasks= new List<TreeDataFormat>() {
new TreeDataFormat() { orderID= "2.1", orderName= "Tilapias", category= "Frozen seafood", units= 278,
orderDate= new DateTime(2017, 03, 05), shippedDate= new DateTime(2019, 03, 15), unitPrice= 15, price= 55.50 },
new TreeDataFormat() { orderID= "2.2", orderName= "White Shrimp", category= "Frozen seafood", units= 560,
orderDate= new DateTime(2017, 05, 07), shippedDate= new DateTime(2019, 09, 19), unitPrice= 7, price= 41.70 },
new TreeDataFormat() { orderID= "2.3", orderName= "Fresh Cheese", category= "Dairy", units= 323,
orderDate= new DateTime(2017, 03, 09), shippedDate= new DateTime(2019, 11, 13), unitPrice= 8, price= 39.20 },
new TreeDataFormat() { orderID= "2.4", orderName= "Blue Veined Cheese", category= "Dairy", units= 373,
orderDate= new DateTime(2017, 03, 11), shippedDate= new DateTime(2019, 11, 13), unitPrice= 9, price= 38.76 },
new TreeDataFormat() { orderID= "2.5", orderName= "Butter", category= "Dairy", units= 413,
orderDate= new DateTime(2017, 12, 23), shippedDate= new DateTime(2019, 12, 23), unitPrice= 7, price= 37.17 }
}
},
new TreeDataFormat()
{
orderID= "3",
orderName= "Order 3",
orderDate= new DateTime(2017, 03, 10),
shippedDate= new DateTime(2019, 05, 20),
units = 1120,
unitPrice = 33,
price = 108.80,
category= "Seafood",
subTasks= new List<TreeDataFormat>() {
new TreeDataFormat() { orderID= "2.1", orderName= "Lead glassware", category= "Solid crystals", units= 542,
orderDate= new DateTime(2017, 03, 05), shippedDate= new DateTime(2019, 03, 15), unitPrice= 6, price= 32.52 },
new TreeDataFormat() { orderID= "3.2", orderName= "Glassware", category= "Solid crystals", units= 324,
orderDate= new DateTime(2017, 05, 07), shippedDate= new DateTime(2019, 09, 19), unitPrice= 11, price= 35.64 },
new TreeDataFormat() { orderID= "3.3", orderName= "Glass beads", category= "Solid crystals", units= 254,
orderDate= new DateTime(2017, 03, 09), shippedDate= new DateTime(2019, 11, 13), unitPrice= 16, price= 40.64 },
}
}
};
return data;
}
}
}
}
@* 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>