Example of Overview in ASP.NET MVC Tree Grid Control
This ASP.NET MVC Tree Grid example demonstrates the overview of basic Tree Grid features such as sorting, filtering, conditional formatting, column template and scrolling.
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "Overview",
Data = ViewData["datasource"],
Height = "400px",
ChildMapping = "states",
TreeColumnIndex = 0,
AllowReordering = true,
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
AllowPaging = false,
Selection = UiGridSelection.Single,
OnQueryCellInfo = "queryCellInfo",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "name", HeaderText = "Province", Width = 210 },
new UiGridColumn { Field = "population", HeaderText = "Population (Million)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, ValueAccessor = "populationValue", Width = 210 },
new UiGridColumn { Field = "gdp", HeaderText = "GDP Rate %", Width = 129, AllowFiltering = false },
new UiGridColumn { Field = "rating", HeaderText = "Credit Rating", Width = 160 },
new UiGridColumn { Field = "unemployment", HeaderText = "Unemployment Rate", Width = 190, AllowFiltering = false },
new UiGridColumn { Field = "coordinates", HeaderText = "Coordinates", Width = 220, AllowSorting = false },
new UiGridColumn { Field = "area", HeaderText = "Area", Width = 140 },
new UiGridColumn { Field = "timezone", HeaderText = "Time Zone", Width = 150 }
}
})
<style>
.e-image {
height: 13px;
width: 14px;
vertical-align: middle;
}
.statustemp {
position: relative;
border-radius: 15px;
text-align: center;
background-color: #C3F1D0;
color: #00752F;
width: 47px;
}
.statustemp.e-lowgdp {
background-color: #FFDBDB;
color: #BD0000;
}
td.e-rowcell .statustxt {
color: #398120;
position: relative;
}
td.e-rowcell .statustxt.e-lowgdp {
color: #e60000;
position: relative;
}
.rating .star {
font-size: 132%;
color: lightgrey;
}
.rating .star.checked {
color: #EE9E54;
}
.rating .star:before {
content: '★';
}
.negativeTimeZone {
transform: rotate(180deg);
}
#myBar {
position: absolute;
width: 10%;
height: 100%;
background-color: #00b300;
}
#myBar.progressdisable {
background-color: #df2222;
}
.material3-dark .rating .star.checked, .material3 .rating .star.checked {
color: #6750A4;
}
.material3-dark #myBar, .material3 #myBar {
background-color: #53CA17;
}
.material3-dark #myBar.progressdisable, .material3 #myBar.progressdisable {
background-color: rgba(242, 184, 181, 1);
}
#treegridlabel {
position: relative;
left: 10px;
line-height: 18px;
font-size: 10px;
color: white;
}
.highcontrast .e-grid #myProgress {
background-color: whitesmoke;
}
#myProgress {
position: relative;
height: 18px;
width: 10em;
text-align: left;
background-color: whitesmoke;
}
.tailwind .e-grid #coordinates.e-checkbox-wrapper .e-label, .e-css.e-checkbox-wrapper .e-label,
.tailwind-dark .e-grid #coordinates.e-checkbox-wrapper .e-label, .e-css.e-checkbox-wrapper .e-label {
display: inline;
}
#coordinates .e-image {
filter: brightness(180%);
}
#myBar.progressdisable {
background-color: #df2222;
}
.e-bigger .e-grid .e-rowcell {
line-height: 24px;
}
</style>
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "Overview",
Data = ViewData["datasource"],
Height = "400px",
ChildMapping = "states",
TreeColumnIndex = 0,
AllowReordering = true,
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
AllowPaging = false,
Selection = UiGridSelection.Single,
OnQueryCellInfo = "queryCellInfo",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "name", HeaderText = "Province", Width = 210 },
new UiGridColumn { Field = "population", HeaderText = "Population (Million)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, ValueAccessor = "populationValue", Width = 210 },
new UiGridColumn { Field = "gdp", HeaderText = "GDP Rate %", Width = 129, AllowFiltering = false },
new UiGridColumn { Field = "rating", HeaderText = "Credit Rating", Width = 160 },
new UiGridColumn { Field = "unemployment", HeaderText = "Unemployment Rate", Width = 190, AllowFiltering = false },
new UiGridColumn { Field = "coordinates", HeaderText = "Coordinates", Width = 220, AllowSorting = false },
new UiGridColumn { Field = "area", HeaderText = "Area", Width = 140 },
new UiGridColumn { Field = "timezone", HeaderText = "Time Zone", Width = 150 }
}
})
<div style="padding-top: 5px; padding-bottom: 20px; text-align: right">
Reference links:
<a href="https://countryeconomy.com/ratings" rel="nofollow" style="padding-right: 12px">https://countryeconomy.com</a>
<a href="https://en.wikipedia.org/wiki/" rel="nofollow" style="padding-right: 12px">https://en.wikipedia.org/wiki/</a>
<a href="http://worldpopulationreview.com" rel="nofollow">http://worldpopulationreview.com</a>
</div>
@{
var countryOf = new Dictionary<string, string>();
foreach (dynamic country in (IEnumerable<object>)ViewData["datasource"])
{
foreach (dynamic state in country.states) { countryOf[(string)state.name] = (string)country.name; }
}
}
<script>
var imageFolder = '@Url.Content("~/Content/images/treegrid/")';
// The country each state belongs to (its flag is the country's), from the data the tree was given.
var countryOf = @Html.Raw(UiJson.Serialize(countryOf));
function populationValue(field, data) {
return data[field] / 1000000;
}
function image(file, alt) {
var img = document.createElement('img');
img.src = imageFolder + file;
img.className = 'e-image';
img.alt = alt;
return img;
}
// Syncfusion's cell templates (flag, GDP, rating, unemployment, coordinates, area, time zone) as elements.
function queryCellInfo(args) {
var d = args.data;
var box;
switch (args.column) {
case 'name':
box = document.createElement('span');
var flag = document.createElement('div');
flag.className = 'image';
flag.style.display = 'inline-block';
flag.appendChild(image((countryOf[d.name] || d.name) + '.png', d.name));
var label = document.createElement('div');
label.style.cssText = 'display:inline-block; padding-left:4px; vertical-align: middle;';
label.textContent = d.name;
box.appendChild(flag);
box.appendChild(label);
args.content = box;
break;
case 'gdp':
box = document.createElement('div');
box.className = 'statustemp' + (d.gdp < 2 ? ' e-lowgdp' : '');
var text = document.createElement('span');
text.className = 'statustxt' + (d.gdp < 2 ? ' e-lowgdp' : '');
text.textContent = d.gdp + ' %';
box.appendChild(text);
args.content = box;
break;
case 'rating':
if (d.rating) {
box = document.createElement('div');
box.className = 'rating';
for (var i = 0; i < 5; i++) {
var star = document.createElement('span');
star.className = 'star' + (i < d.rating ? ' checked' : '');
box.appendChild(star);
}
args.content = box;
}
break;
case 'unemployment':
box = document.createElement('div');
box.className = 'pbar';
var bar = document.createElement('div');
bar.className = 'bar' + (d.unemployment <= 4 ? ' progressdisable' : '');
bar.style.width = d.unemployment * 10 + '%';
var barLabel = document.createElement('div');
barLabel.className = 'barlabel';
barLabel.textContent = d.unemployment + '%';
bar.appendChild(barLabel);
box.appendChild(bar);
args.content = box;
break;
case 'coordinates':
box = document.createElement('div');
box.className = 'image';
box.appendChild(image('Map.png', 'Location'));
box.appendChild(document.createTextNode(' '));
var link = document.createElement('a');
link.target = '_blank';
link.rel = 'nofollow noopener';
link.href = 'https://www.google.com/maps/place/' + encodeURIComponent(d.coordinates);
link.setAttribute('aria-label', 'google maps');
link.textContent = d.coordinates;
box.appendChild(link);
args.content = box;
break;
case 'area':
box = document.createElement('span');
box.appendChild(document.createTextNode(d.area + ' km'));
var sup = document.createElement('sup');
sup.textContent = '2';
box.appendChild(sup);
args.content = box;
break;
case 'timezone':
box = document.createElement('span');
var zone = image('__Normal.png', 'Timezone');
zone.style.filter = 'brightness(150%)';
if (String(d.timezone).indexOf('-') !== -1) { zone.className += ' negativeTimeZone'; }
box.appendChild(zone);
var zoneText = document.createElement('span');
zoneText.style.paddingLeft = '7px';
zoneText.textContent = d.timezone;
box.appendChild(zoneText);
args.content = box;
break;
}
}
</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/Overview.cshtml; it draws what ViewData holds.
public ActionResult Overview()
{
var treeData = TreeOverviewData.GetCountriesData();
ViewData["datasource"] = treeData;
return View();
}
public class TreeOverviewData
{
public TreeOverviewData() { }
public string name
{
get;
set;
}
public string capital
{
get;
set;
}
public double area
{
get;
set;
}
public double population
{
get;
set;
}
public double gdp
{
get;
set;
}
public string timezone
{
get;
set;
}
public double rating
{
get;
set;
}
public double unemployment
{
get;
set;
}
public string coordinates
{
get;
set;
}
public List<TreeOverviewData> states { get; set; }
public static List<TreeOverviewData> GetCountriesData()
{
List<TreeOverviewData> data = new List<TreeOverviewData>()
{
new TreeOverviewData()
{
name= "USA",
capital = "Washington, D.C.",
area = 9147590,
population = 327892000,
gdp = 2.2,
timezone= "UTC -5 to -10",
rating = 5,
unemployment = 3.9,
coordinates = "37.0902° N, 95.7129° W",
states= new List<TreeOverviewData>() {
new TreeOverviewData() { name= "Washington, D.C.", area= 184827, population= 693972, gdp= 4.7,
timezone= "UTC -5", unemployment= 4.3, coordinates= "38.9072° N, 77.0369° W" },
new TreeOverviewData() { name= "New York", area= 783.8, capital= "Albany", population= 8175133, gdp= 1.9,
timezone= "UTC -5", unemployment= 3.9, coordinates= "40.7128° N, 74.0060° W" },
new TreeOverviewData() { name= "New Mexico", area= 315194, capital= "Santa Fe", population= 2088070, gdp= 0.1,
timezone= "UTC -7", unemployment= 4.7, coordinates= "34.5199° N, 105.8701° W" },
new TreeOverviewData() { name= "Alaska", area= 1717856, capital= "Juneau", population= 297832, gdp= -0.5,
timezone= "UTC -9", unemployment= 6.3, coordinates= "64.2008° N, 149.4937° W" }
}
},
new TreeOverviewData()
{
name= "Greece",
capital = "Athens",
area = 131957,
population = 10783625,
gdp = 1.5,
timezone= "UTC +2.0",
rating = 3,
unemployment = 20.8,
coordinates = "39.0742° N, 21.8243° E",
states= new List<TreeOverviewData>() {
new TreeOverviewData() { name= "Athens", area= 2929, population= 664046, gdp= 1,
timezone= "UTC +2.0", unemployment= 7.7, coordinates= "37.9838° N, 23.7275° E" },
new TreeOverviewData() { name= "Arcadia", capital= "Tripoli", area= 28.83, population= 58799, gdp= 2.5,
timezone= "UTC +2.0", unemployment= 3.0, coordinates= "34.1397° N, 118.0353° W" },
new TreeOverviewData() { name= "Argolis", capital= "Nafplio", area= 2154, population= 97044, gdp= 2.1,
timezone= "UTC +2.0", unemployment= 6.2, coordinates= "37.6525° N, 22.8582° E" },
}
},
new TreeOverviewData()
{
name= "Germany",
capital = "Berlin",
area = 357386,
population = 82293457,
gdp = 2.2,
timezone= "UTC +1.0",
rating = 5,
unemployment = 3.3,
coordinates = "51.1657° N, 10.4515° E",
states= new List<TreeOverviewData>() {
new TreeOverviewData() { name= "Berlin", area= 891.8, population= 3539234, gdp= 4.1,
timezone= "UTC +1.0", unemployment= 7.7, coordinates= "52.5200° N, 13.4050° E" },
new TreeOverviewData() { name= "Bavaria", capital= "Munich", area= 70550, population= 12997204, gdp= 3.1,
timezone= "UTC +1.0", unemployment= 2.7, coordinates= "48.7904° N, 11.4979° E" },
new TreeOverviewData() { name= "Saxony", capital= "Dresden", area= 18416, population= 4081308, gdp= 3.8,
timezone= "UTC +1.0", unemployment= 6.2, coordinates= "51.1045° N, 13.2017° E" },
}
},
new TreeOverviewData()
{
name= "Bangladesh",
capital = "Dhaka",
area = 147570,
population = 185584811,
gdp = 7.3,
timezone = "UTC +6.0",
rating = 3,
unemployment = 4.3,
coordinates = "23.6850° N, 90.3563° E",
states= new List<TreeOverviewData>() {
new TreeOverviewData() { name= "Dhaka", area= 306.4, population= 10356500, gdp= 7.28,
timezone= "UTC +6.0", unemployment= 7.4, coordinates= "23.8103° N, 90.4125° E" },
new TreeOverviewData() { name= "Barisal", capital= "Barisal", area= 16.37, population= 202242, gdp= 6.3,
timezone= "UTC +6.0", unemployment= 5, coordinates= "22.7010° N, 90.3535° E" },
new TreeOverviewData() { name= "Chittagong", capital= "Chittagong", area= 168.1, population= 3920222, gdp= 6.3,
timezone= "UTC +6.0", unemployment= 4.7, coordinates= "22.3569° N, 91.7832° E" },
}
},
new TreeOverviewData()
{
name= "Egypt",
capital = "Cairo",
area = 1001449,
population = 99375741,
gdp = 4.2,
timezone= "UTC +2.0",
rating = 3,
unemployment = 9.9,
coordinates = "26.8206° N, 30.8025° E",
states= new List<TreeOverviewData>() {
new TreeOverviewData() { name= "Cairo", area= 528, population= 7734614, gdp= 3.7,
timezone= "UTC +2.0", unemployment= 11.3, coordinates= "30.0444° N, 31.2357° E" },
new TreeOverviewData() { name= "Alexandria", capital= "Bacos", area= 46.42, population= 3811516, gdp= 4.2,
timezone= "UTC +2.0", unemployment= 5.3, coordinates= "31.2001° N, 29.9187° E" },
new TreeOverviewData() { name= "Giza", capital= "Giza", area= 1580, population= 2443203, gdp= 4.1,
timezone= "UTC +2.0", unemployment= 4.7, coordinates= "30.0131° N, 31.2089° E" },
}
},
new TreeOverviewData()
{
name= "Canada",
capital = "Ottawa",
area = 9984670,
population = 36953765,
gdp = 3.0,
timezone= "UTC -3.3 to -8",
rating = 5,
unemployment = 5.8,
coordinates = "56.1304° N, 106.3468° W",
states= new List<TreeOverviewData>() {
new TreeOverviewData() { name= "Ontario", capital= "Toronto", area= 908607, population= 14374084, gdp= 2.8,
timezone= "UTC -5", unemployment= 5.9, coordinates= "51.2538° N, 85.3232° W" },
new TreeOverviewData() { name= "Quebec", capital= "Quebec", area= 1542056, population= 8455402, gdp= 1.9,
timezone= "UTC -4 to -5", unemployment= 5.6, coordinates= "46.8139° N, 71.2080° W" },
new TreeOverviewData() { name= "Alberta", capital= "Edmonton", area= 661848, population= 4334025, gdp= 1.6,
timezone= "UTC -7", unemployment= 7.8, coordinates= "53.9333° N, 116.5765° W" },
new TreeOverviewData() { name= "Manitoba", capital= "Winnipeg", area= 647797, population= 1348809, gdp= 2.9,
timezone= "UTC -6", unemployment= 6, coordinates= "53.7609° N, 98.8139° W" },
}
},
};
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>