Example of Column Formatting in ASP.NET MVC Tree Grid Control
This sample demonstrates the way of displaying the content of Tree Grid columns based on the specified format. In this sample, format of columns can be changed dynamically through property panel.
Properties
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "TreeGrid",
Data = ViewData["datasource"],
AllowPaging = true,
PageSize = 12,
ChildMapping = "subTasks",
Height = "350px",
TreeColumnIndex = 1,
Selection = UiGridSelection.Single,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "orderID", HeaderText = "Order ID", Type = UiColumnType.Number, Width = 110, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "orderName", HeaderText = "Order Name", Width = 210 },
new UiGridColumn { Field = "shippedDate", HeaderText = "Shipped Date", Type = UiColumnType.Date, Format = "M/d/yyyy", TextAlign = UiTextAlign.Right, Width = 190 },
new UiGridColumn { Field = "price", HeaderText = "Price", Type = UiColumnType.Number, Format = "C2", TextAlign = UiTextAlign.Right, Width = 120 }
}
})
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "TreeGrid",
Data = ViewData["datasource"],
AllowPaging = true,
PageSize = 12,
ChildMapping = "subTasks",
Height = "350px",
TreeColumnIndex = 1,
Selection = UiGridSelection.Single,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "orderID", HeaderText = "Order ID", Type = UiColumnType.Number, Width = 110, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "orderName", HeaderText = "Order Name", Width = 210 },
new UiGridColumn { Field = "shippedDate", HeaderText = "Shipped Date", Type = UiColumnType.Date, Format = "M/d/yyyy", TextAlign = UiTextAlign.Right, Width = 190 },
new UiGridColumn { Field = "price", HeaderText = "Price", Type = UiColumnType.Number, Format = "C2", TextAlign = UiTextAlign.Right, Width = 120 }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="Columns">Column</label><select id="Columns">@foreach (var option in (List<KeyValuePair<string, string>>)ViewData["columns"]) { <option value="@option.Value">@option.Key</option> }</select></div>
<div class="demo-property-row"><label for="Formats">Format</label><select id="Formats">@foreach (var option in (List<KeyValuePair<string, string>>)ViewData["numberFormats"]) { <option value="@option.Value">@option.Key</option> }</select></div>
</div>
<script>
var priceFormats = [['n2', 'N2'], ['n3', 'N3'], ['c2', 'C2'], ['c3', 'C3'], ['p2', 'P2'], ['p3', 'P3']];
var dateFormats = [['Short Date', 'M/d/yyyy'], ['Long Date', 'dddd, MMMM dd, yyyy'], ['Month/Year', 'MMMM, yyyy'], ['Month/Day', 'MMMM, dd']];
var formatTree = function () { return window.Lumen.get('TreeGrid'); };
function fillFormats(list, current) {
var select = document.getElementById('Formats');
select.innerHTML = '';
list.forEach(function (pair) {
var option = document.createElement('option');
option.value = pair[1];
option.textContent = pair[0];
select.appendChild(option);
});
select.value = current;
}
// Syncfusion: choosing a column shows its format in the list.
document.getElementById('Columns').addEventListener('change', function (e) {
fillFormats(e.target.value === 'price' ? priceFormats : dateFormats, formatTree().getColumnFormat(e.target.value));
});
// Syncfusion: column.format = the format, then treegrid.refreshColumns().
document.getElementById('Formats').addEventListener('change', function (e) {
formatTree().setColumnFormat(document.getElementById('Columns').value, e.target.value);
});
document.getElementById('Columns').value = 'price';
fillFormats(priceFormats, 'C2');
</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/ColumnFormatting.cshtml; it draws what ViewData holds.
public ActionResult ColumnFormatting()
{
var treeData = TreeDataFormat.GetDataFormat();
ViewData["datasource"] = treeData;
ViewData["columns"] = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Price", "price"),
new KeyValuePair<string, string>("Shipped Date", "shippedDate")
};
ViewData["numberFormats"] = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("n2", "N2")
};
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>
Format is the process of customizing the particular column data/values based on specific culture. The Tree Grid uses Internalization library to format number and date values. The format can be specified by using Format property of Columns.
In this demo, select the column and format from the property panel to format the corresponding column values..