Example of Header Template in ASP.NET MVC Tree Grid Control
This sample demonstrates the Tree Grid header template feature. In this sample, we have shown custom icons in the column headers.
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "TreeGrid",
Data = ViewData["datasource"],
Height = "410px",
ChildMapping = "Children",
TreeColumnIndex = 0,
AllowPaging = false,
Selection = UiGridSelection.Single,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "Name", HeaderTemplate = "#projectName" },
new UiGridColumn { Field = "StartDate", HeaderTemplate = "#dateTemplate", Type = UiColumnType.Date, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "Resources", HeaderTemplate = "#resource", TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Duration", HeaderTemplate = "#durationTemplate", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "PercentDone", HeaderTemplate = "#progressTemplate", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right }
}
})
<style>
.material-dark img.resources, .material3-dark img.resources, .fabric-dark img.resources, .bootstrap-dark img.resources, .bootstrap4-dark img.resources, .bootstrap5-dark img.resources, .bootstrap5\.3-dark img.resources, .highcontrast img.resources,
.tailwind-dark img.resources, .tailwind3-dark img.resources, .fluent-dark img.resources, .fluent2-dark img.resources, .fluent2-highcontrast img.resources {
content: url('@Url.Content("~/Content/images/treegrid/darkResources.png")');
}
.material-dark img.taskname, .material3-dark img.taskname, .fabric-dark img.taskname, .bootstrap-dark img.taskname, .bootstrap4-dark img.taskname, .bootstrap5-dark img.taskname, .bootstrap5\.3-dark img.taskname, .highcontrast img.taskname,
.tailwind-dark img.taskname, .tailwind3-dark img.taskname, .fluent-dark img.taskname, .fluent2-dark img.taskname, .fluent2-highcontrast img.taskname {
content: url('@Url.Content("~/Content/images/treegrid/darkTaskname.png")');
}
.material-dark img.startdate, .material3-dark img.startdate, .fabric-dark img.startdate, .bootstrap-dark img.startdate, .bootstrap4-dark img.startdate, .bootstrap5-dark img.startdate, .bootstrap5\.3-dark img.startdate, .highcontrast img.startdate,
.tailwind-dark img.startdate, .tailwind3-dark img.startdate, .fluent-dark img.startdate, .fluent2-dark img.startdate, .fluent2-highcontrast img.startdate {
content: url('@Url.Content("~/Content/images/treegrid/darkStartname.png")');
}
.material-dark img.duration, .material3-dark img.duration, .fabric-dark img.duration, .bootstrap-dark img.duration, .bootstrap4-dark img.duration, .bootstrap5-dark img.duration, .bootstrap5\.3-dark img.duration, .highcontrast img.duration,
.tailwind-dark img.duration, .tailwind3-dark img.duration, .fluent-dark img.duration, .fluent2-dark img.duration, .fluent2-highcontrast img.duration {
content: url('@Url.Content("~/Content/images/treegrid/darkduration.png")');
}
.material-dark img.Progress, .material3-dark img.Progress, .fabric-dark img.Progress, .bootstrap-dark img.Progress, .bootstrap4-dark img.Progress, .bootstrap5-dark img.Progress, .bootstrap5\.3-dark img.Progress, .highcontrast img.Progress,
.tailwind-dark img.Progress, .tailwind3-dark img.Progress, .fluent-dark img.Progress, .fluent2-dark img.Progress, .fluent2-highcontrast img.Progress {
content: url('@Url.Content("~/Content/images/treegrid/darkprogress.png")');
}
.bootstrap5 img.taskname, .bootstrap5 img.duration, .bootstrap5 img.Progress, .bootstrap5\.3 img.taskname, .bootstrap5\.3 img.duration, .bootstrap5\.3 img.Progress, .tailwind img.resources, .tailwind img.taskname, .tailwind img.startdate,
.tailwind img.duration, .tailwind img.Progress {
padding-bottom: 3px !important;
}
.bootstrap5 img.resources, .bootstrap5 img.startdate, .bootstrap5\.3 img.resources, .bootstrap5\.3 img.startdate {
padding-bottom: 2px !important;
}
.bootstrap4 img.startdate, .material img.startdate, .bootstrap img.startdate, .bootstrap5\.3 img.startdate {
padding-bottom: 1px;
}
</style>
<template id="resource"><div>
<div>
<img src="@Url.Content("~/Content/images/treegrid/Resources.png")" alt="Resource" width="20" height="20" style="padding-bottom: 1px" class="resources" />
<span> Resources</span>
</div>
</div></template>
<template id="projectName"><div>
<div>
<img src="@Url.Content("~/Content/images/treegrid/Taskname.png")" alt="Taskname" width="20" height="20" style="padding-bottom: 1px" class="taskname" />
<span> Task Name</span>
</div>
</div></template>
<template id="dateTemplate"><div>
<div>
<img src="@Url.Content("~/Content/images/treegrid/Startdate.png")" alt="StartDate" width="20" height="20" class="startdate" />
<span> Start Date</span>
</div>
</div></template>
<template id="durationTemplate"><div>
<div>
<img src="@Url.Content("~/Content/images/treegrid/duration.png")" alt="Duration" width="20" height="20" style="padding-bottom: 2px" class="duration" />
<span> Duration</span>
</div>
</div></template>
<template id="progressTemplate"><div>
<div>
<img src="@Url.Content("~/Content/images/treegrid/progress.png")" alt="Progress" width="20" height="20" style="padding-bottom: 2px" class="Progress" />
<span> Progress</span>
</div>
</div></template>
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "TreeGrid",
Data = ViewData["datasource"],
Height = "410px",
ChildMapping = "Children",
TreeColumnIndex = 0,
AllowPaging = false,
Selection = UiGridSelection.Single,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "Name", HeaderTemplate = "#projectName" },
new UiGridColumn { Field = "StartDate", HeaderTemplate = "#dateTemplate", Type = UiColumnType.Date, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "Resources", HeaderTemplate = "#resource", TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Duration", HeaderTemplate = "#durationTemplate", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "PercentDone", HeaderTemplate = "#progressTemplate", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right }
}
})
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/HeaderTemplate.cshtml; it draws what ViewData holds.
public ActionResult HeaderTemplate()
{
var treeData = TreeGridHeader.GetDataSource();
ViewData["datasource"] = treeData;
return View();
}
public class TreeGridHeader
{
public DateTime StartDate { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public int Duration { get; set; }
public int PercentDone { get; set; }
public List<TreeGridHeader> Children { get; set; }
public string Resources { get; set; }
public static List<TreeGridHeader> GetDataSource()
{
List<TreeGridHeader> BusinessObjectCollection = new List<TreeGridHeader>();
BusinessObjectCollection.Add(new TreeGridHeader()
{
Id = 1,
Name = "Task 1",
StartDate = new DateTime(2017, 10, 23),
Duration = 10,
PercentDone = 70,
Resources = "1",
Children = (new List<TreeGridHeader>()
{
new TreeGridHeader()
{
Id = 2,
Name = "Child task 1",
StartDate = new DateTime(2017, 10, 23),
Duration = 4,
PercentDone = 80,
Resources ="4"
},
new TreeGridHeader()
{
Id = 3,
Name = "Child Task 2",
StartDate = new DateTime(2017, 10, 24),
Duration = 5,
PercentDone = 65,
Resources ="1"
},
new TreeGridHeader()
{
Id = 4,
Name = "Child task 3",
StartDate = new DateTime(2017, 10, 25),
Duration = 6,
PercentDone = 77,
Resources ="5",
Children = (new List<TreeGridHeader>()
{
new TreeGridHeader()
{
Id = 5,
Name = "Grand child task 1",
StartDate = new DateTime(2017, 10, 28),
Duration = 5,
PercentDone = 60,
Resources ="7",
},
new TreeGridHeader()
{
Id = 6,
Name = "Grand child task 2",
StartDate = new DateTime(2017, 10, 29),
Duration = 6,
PercentDone = 77,
Resources ="7",
},
new TreeGridHeader()
{
Id = 7,
Name = "Grand child task 3",
StartDate = new DateTime(2017, 10, 29),
Duration = 3,
PercentDone = 77,
Resources ="8"
}
})
}
})
});
BusinessObjectCollection.Add(new TreeGridHeader()
{
Id = 8,
Name = "Task 2",
StartDate = new DateTime(2017, 10, 23),
Duration = 10,
PercentDone = 70,
Resources = "5",
Children = (new List<TreeGridHeader>()
{
new TreeGridHeader()
{
Id = 9,
Name = "Child task 1",
StartDate = new DateTime(2017, 10, 23),
Duration = 4,
PercentDone = 80,
Resources ="4",
},
new TreeGridHeader()
{
Id = 10,
Name = "Child Task 2",
StartDate = new DateTime(2017, 10, 24),
Duration = 5,
PercentDone = 65,
Resources ="1",
},
new TreeGridHeader()
{
Id = 11,
Name = "Child task 3",
StartDate = new DateTime(2017, 10, 25),
Duration = 6,
PercentDone = 77,
Resources ="5",
Children = (new List<TreeGridHeader>()
{
new TreeGridHeader()
{
Id = 12,
Name = "Grand child task 1",
StartDate = new DateTime(2017, 10, 28),
Duration = 5,
PercentDone = 60,
Resources ="7",
},
new TreeGridHeader()
{
Id = 13,
Name = "Grand child task 2",
StartDate = new DateTime(2017, 10, 29),
Duration = 6,
PercentDone = 77,
Resources ="7",
},
new TreeGridHeader()
{
Id = 14,
Name = "Grand child task 3",
StartDate = new DateTime(2017, 10, 29),
Duration = 3,
PercentDone = 88,
Resources ="8"
}
})
}
})
});
return BusinessObjectCollection;
}
}
}
}
@* 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>