Example of Custom Aggregate in ASP.NET MVC Tree Grid Control
This sample demonstrates custom aggregates and exporting functionality in the Tree Grid. Aggregate values for the columns are displayed in the column footer, and export options are available via the toolbar buttons.
Count of : ${Custom}
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "CustomAggregate",
OnDataLoaded = "ondataBound",
Data = ViewData["datasource"],
ChildMapping = "Subtasks",
GridLines = UiGridLines.Both,
Height = "400px",
TreeColumnIndex = 1,
AllowPaging = false,
Selection = UiGridSelection.Single,
Toolbar = new List<string> { "ExcelExport", "PdfExport", "CsvExport" },
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "Category", Type = UiAggregateType.Custom, CustomAggregate = "customCategoryAggregateFn", FooterTemplate = "#aggFooterCategoryCustom" }
}
}
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ID", HeaderText = "Order ID", TextAlign = UiTextAlign.Left },
new UiGridColumn { Field = "Name", HeaderText = "Shipment Name", ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "ShipmentDate", HeaderText = "Shipment Date", Type = UiColumnType.Date, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "Category", HeaderText = "Category" },
new UiGridColumn { Field = "Units", HeaderText = "Units", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Unit Price($)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Format = "C0" },
new UiGridColumn { Field = "Price", HeaderText = "Price($)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Format = "C0" }
}
})
<template id="aggFooterCategoryCustom">Count of <select id="customers"></select> : ${Custom}</template>
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "CustomAggregate",
OnDataLoaded = "ondataBound",
Data = ViewData["datasource"],
ChildMapping = "Subtasks",
GridLines = UiGridLines.Both,
Height = "400px",
TreeColumnIndex = 1,
AllowPaging = false,
Selection = UiGridSelection.Single,
Toolbar = new List<string> { "ExcelExport", "PdfExport", "CsvExport" },
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "Category", Type = UiAggregateType.Custom, CustomAggregate = "customCategoryAggregateFn", FooterTemplate = "#aggFooterCategoryCustom" }
}
}
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ID", HeaderText = "Order ID", TextAlign = UiTextAlign.Left },
new UiGridColumn { Field = "Name", HeaderText = "Shipment Name", ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "ShipmentDate", HeaderText = "Shipment Date", Type = UiColumnType.Date, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "Category", HeaderText = "Category" },
new UiGridColumn { Field = "Units", HeaderText = "Units", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Unit Price($)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Format = "C0" },
new UiGridColumn { Field = "Price", HeaderText = "Price($)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Format = "C0" }
}
})
<script>
var value = 'Seafood';
var foods = ['Seafood', 'Dairy', 'Edible', 'Crystal'];
// Syncfusion: a DropDownList in the footer picks the category; its change reads the rows again.
function ondataBound() {
var list = document.getElementById('customers');
if (!list) { return; }
list.innerHTML = '';
foods.forEach(function (food) {
var option = document.createElement('option');
option.value = food;
option.textContent = food;
list.appendChild(option);
});
list.value = value;
list.onchange = function () {
value = list.value;
window.Lumen.get('CustomAggregate').refresh();
};
}
// The custom aggregate: how many rows are of the chosen category.
function customCategoryAggregateFn(rows) {
return rows.filter(function (item) { return item.Category === value; }).length;
}
</script>
<script>
window.TreeAggregate = (function () {
'use strict';
/** CustomAggregate: the products (rows with a number of units) that have fewer than 20 units in stock. */
function lowStock(rows) {
return rows.filter(function (r) { return typeof r.Units === 'number' && r.Units < 20; }).length;
}
return { lowStock: lowStock };
})();
</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/CustomAggregate.cshtml; it draws what ViewData holds.
public ActionResult CustomAggregate()
{
var treeData = OrdersTreeData.GetShipmentData();
ViewData["datasource"] = treeData;
return View();
}
public class OrdersTreeData
{
public string ID { get; set; }
public string Name { get; set; }
public int Units { get; set; }
public string Category { get; set; }
public int UnitPrice { get; set; }
public int Price { get; set; }
public DateTime ShipmentDate { get; set; }
public List<OrdersTreeData> Subtasks { get; set; }
public static List<OrdersTreeData> GetShipmentData()
{
List<OrdersTreeData> DataCollection = new List<OrdersTreeData>();
OrdersTreeData Parent1 = new OrdersTreeData()
{
ID = "ORD-55721",
Name = "Seafood Order",
Category = "Seafoods",
Units = 1395,
UnitPrice = 47,
Price = 65565,
ShipmentDate = new DateTime(2022, 02, 11),
Subtasks = new List<OrdersTreeData>()
};
OrdersTreeData Child1 = new OrdersTreeData()
{
ID = "ORD-55722",
Name = "Mackerel",
Category = "Seafood",
Units = 235,
UnitPrice = 12,
Price = 2820,
ShipmentDate = new DateTime(2022, 01, 05),
};
OrdersTreeData Child2 = new OrdersTreeData()
{
ID = "ORD-55723",
Name = "Yellowfin Tuna",
Category = "Seafood",
Units = 324,
UnitPrice = 8,
Price = 2592,
ShipmentDate = new DateTime(2022, 01, 17),
};
OrdersTreeData Child3 = new OrdersTreeData()
{
ID = "ORD-55724",
Name = "Herrings",
Category = "Seafood",
Units = 488,
UnitPrice = 11,
Price = 5368,
ShipmentDate = new DateTime(2022, 04, 16),
};
OrdersTreeData Child4 = new OrdersTreeData()
{
ID = "ORD-55725",
Name = "Preserved Olives",
Category = "Edible",
Units = 125,
UnitPrice = 9,
Price = 1125,
ShipmentDate = new DateTime(2022, 07, 11),
};
OrdersTreeData Child5 = new OrdersTreeData()
{
ID = "ORD-55726",
Name = " Sweet Corn Frozen ",
Category = "Edible",
Units = 223,
UnitPrice = 7,
Price = 1561,
ShipmentDate = new DateTime(2022, 07, 27),
};
Parent1.Subtasks.Add(Child1);
Parent1.Subtasks.Add(Child2);
Parent1.Subtasks.Add(Child3);
Parent1.Subtasks.Add(Child4);
Parent1.Subtasks.Add(Child5);
OrdersTreeData Parent2 = new OrdersTreeData()
{
ID = "ORD-55727",
Name = "Products Order",
Category = "Products",
Units = 1944,
UnitPrice = 58,
Price = 21233,
ShipmentDate = new DateTime(2023, 04, 25),
Subtasks = new List<OrdersTreeData>()
};
OrdersTreeData Child6 = new OrdersTreeData()
{
ID = "ORD-55728",
Name = "Tilapias",
Category = "Seafood",
Units = 278,
UnitPrice = 15,
Price = 4170,
ShipmentDate = new DateTime(2023, 05, 12),
};
OrdersTreeData Child7 = new OrdersTreeData()
{
ID = "ORD-55729",
Name = "White Shrimp",
Category = "Seafood",
Units = 560,
UnitPrice = 7,
Price = 3920,
ShipmentDate = new DateTime(2023, 07, 23),
};
OrdersTreeData Child8 = new OrdersTreeData()
{
ID = "ORD-55730",
Name = "Fresh Cheese",
Category = "Dairy",
Units = 323,
UnitPrice = 12,
Price = 3876,
ShipmentDate = new DateTime(2023, 07, 30),
};
OrdersTreeData Child9 = new OrdersTreeData()
{
ID = "ORD-55731",
Name = "Blue Veined Cheese",
Category = "Dairy",
Units = 370,
UnitPrice = 15,
Price = 5550,
ShipmentDate = new DateTime(2023, 10, 15),
};
OrdersTreeData Child10 = new OrdersTreeData()
{
ID = "ORD-55732",
Name = "Butter",
Category = "Dairy",
Units = 413,
UnitPrice = 9,
Price = 3717,
ShipmentDate = new DateTime(2023, 10, 30),
};
Parent2.Subtasks.Add(Child6);
Parent2.Subtasks.Add(Child7);
Parent2.Subtasks.Add(Child8);
Parent2.Subtasks.Add(Child9);
Parent2.Subtasks.Add(Child10);
OrdersTreeData Parent3 = new OrdersTreeData()
{
ID = "ORD-55733",
Name = "Crystals Order",
Category = "Crystals",
Units = 1944,
UnitPrice = 58,
Price = 21233,
ShipmentDate = new DateTime(2024, 02, 28),
Subtasks = new List<OrdersTreeData>()
};
OrdersTreeData Child11 = new OrdersTreeData()
{
ID = "ORD-55734",
Name = "Lead Glassware",
Category = "Crystal",
Units = 542,
UnitPrice = 6,
Price = 3252,
ShipmentDate = new DateTime(2024, 06, 08),
};
OrdersTreeData Child12 = new OrdersTreeData()
{
ID = "ORD-55735",
Name = "Pharmaceutical Glass",
Category = "Crystal",
Units = 324,
UnitPrice = 11,
Price = 3564,
ShipmentDate = new DateTime(2024, 06, 16),
};
OrdersTreeData Child13 = new OrdersTreeData()
{
ID = "ORD-55736",
Name = "Glass Beads",
Category = "Crystal",
Units = 254,
UnitPrice = 16,
Price = 4064,
ShipmentDate = new DateTime(2024, 06, 23)
};
Parent3.Subtasks.Add(Child11);
Parent3.Subtasks.Add(Child12);
Parent3.Subtasks.Add(Child13);
DataCollection.Add(Parent1);
DataCollection.Add(Parent2);
DataCollection.Add(Parent3);
return DataCollection;
}
}
}
}
@* 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>
The Tree Grid supports displaying aggregates in the footer, which can be configured using the aggregates property. Here, a CustomAggregate configuration is applied to the Category column to show a dropdown that displays the count of the selected category.
The Tree Grid also supports seamless exports to Excel, PDF, or CSV with a single click. The excelAggregateQueryCellInfo and pdfAggregateQueryCellInfo events ensure that footer aggregate values are acurately preserved in the exported files.