Example of Detail Template Exporting in ASP.NET Core Data Grid Control
This sample demonstrates how to export detail grids. In this sample, you can export the detail grid by clicking the corresponding export button on the grid's toolbar.
| First Name: ${FirstName} | Postal Code: ${PostalCode} | |
| Last Name: ${LastName} | City: ${City} | |
| Title: ${Title} | Phone: ${Phone} | |
| Address: ${Address} | HireDate: ${HireDate} |
@Html.UiGrid(new UiGridOptions
{
Id = "DetailTemplateExport",
Data = ViewData["dataSource"],
DetailTemplate = "#detailtemplate",
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
Toolbar = new List<string> { "ExcelExport", "PdfExport" },
OnToolbarClick = "toolbarClick",
OnExportDetailTemplate = "exportDetailTemplate",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", Type = UiColumnType.Number, Width = 110, TextAlign = UiTextAlign.Right, IsPrimaryKey = true },
new UiGridColumn { Field = "FirstName", HeaderText = "Name", Width = 110 },
new UiGridColumn { Field = "Title", HeaderText = "Title", Width = 240 },
new UiGridColumn { Field = "Country", HeaderText = "Country", Width = 180 }
}
})
<style>
.detailtable td { font-size: 13px; padding: 4px; font-weight: normal; }
.detailtable .detail-label { font-weight: 500; }
</style>
<template id="detailtemplate">
<table class="detailtable" width="100%">
<colgroup><col width="35%" /><col width="35%" /><col width="30%" /></colgroup>
<tbody>
<tr><td><span class="detail-label">First Name: </span> ${FirstName}</td><td><span class="detail-label">Postal Code: </span> ${PostalCode}</td><td></td></tr>
<tr><td><span class="detail-label">Last Name: </span> ${LastName}</td><td><span class="detail-label">City: </span> ${City}</td><td></td></tr>
<tr><td><span class="detail-label">Title: </span> ${Title}</td><td><span class="detail-label">Phone: </span> ${Phone}</td><td></td></tr>
<tr><td><span class="detail-label">Address: </span> ${Address}</td><td><span class="detail-label">HireDate: </span> ${HireDate}</td><td></td></tr>
</tbody>
</table>
</template>
@Html.UiGrid(new UiGridOptions
{
Id = "DetailTemplateExport",
Data = ViewData["dataSource"],
DetailTemplate = "#detailtemplate",
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
Toolbar = new List<string> { "ExcelExport", "PdfExport" },
OnToolbarClick = "toolbarClick",
OnExportDetailTemplate = "exportDetailTemplate",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", Type = UiColumnType.Number, Width = 110, TextAlign = UiTextAlign.Right, IsPrimaryKey = true },
new UiGridColumn { Field = "FirstName", HeaderText = "Name", Width = 110 },
new UiGridColumn { Field = "Title", HeaderText = "Title", Width = 240 },
new UiGridColumn { Field = "Country", HeaderText = "Country", Width = 180 }
}
})
<script>
// Syncfusion: both items export the detail rows too.
function toolbarClick(args) {
var grid = window.Lumen.get('DetailTemplateExport');
if (args.item.id === 'pdfexport') { args.cancel = true; grid.pdfExport({ hierarchyExportMode: 'All' }); }
if (args.item.id === 'excelexport') { args.cancel = true; grid.excelExport({ hierarchyExportMode: 'All' }); }
}
// Syncfusion's exportDetailTemplate: the lines written under each exported row (the photo is left out: it is not part of this demo).
function exportDetailTemplate(args) {
var d = args.parentRow.data;
args.value = { rows: [
['First Name: ' + d.FirstName, 'Postal Code: ' + d.PostalCode],
['Last Name: ' + d.LastName, 'City: ' + d.City],
['Title: ' + d.Title, 'Phone: ' + d.Phone],
['Address: ' + d.Address, 'HireDate: ' + new Date(d.HireDate).toLocaleDateString()]
] };
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class GridController : Controller
{
// The page is Views/Grid/DetailTemplateExporting.cshtml; it draws what ViewData holds.
public ActionResult DetailTemplateExporting()
{
var employee = EmployeeView.GetAllRecords();
ViewData["dataSource"] = employee;
return View();
}
public class EmployeeView
{
public EmployeeView()
{
}
public EmployeeView(int EmployeeID, string FirstName, string LastName, string Title, DateTime BirthDate, DateTime HireDate, int ReportsTo, string Address, string PostalCode, string Phone, string City, string Country)
{
this.EmployeeID = EmployeeID;
this.FirstName = FirstName;
this.LastName = LastName;
this.Title = Title;
this.BirthDate = BirthDate;
this.HireDate = HireDate;
this.ReportsTo = ReportsTo;
this.Address = Address;
this.PostalCode = PostalCode;
this.Phone = Phone;
this.City = City;
this.Country = Country;
}
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public DateTime BirthDate { get; set; }
public DateTime HireDate { get; set; }
public int ReportsTo { get; set; }
public string Address { get; set; }
public string PostalCode { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Country { get; set; }
public static List<EmployeeView> GetAllRecords()
{
List<EmployeeView> Emp = new List<EmployeeView>();
Emp.Add(new EmployeeView(1, "Nancy", "Davolio", "Sales Representative", new DateTime(1948, 12, 08), new DateTime(1992, 05, 01), 2, "507 - 20th Ave. E.Apt. 2A ", " 98122", "(206) 555-9857 ", "Seattle ", "USA"));
Emp.Add(new EmployeeView(2, "Andrew", "Fuller", "Vice President, Sales", new DateTime(1952, 02, 19), new DateTime(1992, 08, 14), 4, "908 W. Capital Way", "98401 ", "(206) 555-9482 ", "Kirkland ", "USA"));
Emp.Add(new EmployeeView(3, "Janet", "Leverling", "Sales Representative", new DateTime(1963, 08, 30), new DateTime(1992, 04, 01), 3, " 4110 Old Redmond Rd.", "98052 ", "(206) 555-8122", "Redmond ", "USA "));
Emp.Add(new EmployeeView(4, "Margaret", "Peacock", "Sales Representative", new DateTime(1937, 09, 19), new DateTime(1993, 05, 03), 6, "14 Garrett Hill ", "SW1 8JR ", "(71) 555-4848 ", "London ", "UK "));
Emp.Add(new EmployeeView(5, "Steven", "Buchanan", "Sales Manager", new DateTime(1955, 03, 04), new DateTime(1993, 10, 17), 8, "Coventry HouseMiner Rd. ", "EC2 7JR ", " (206) 555-8122", "Tacoma ", " USA"));
Emp.Add(new EmployeeView(6, "Michael", "Suyama", "Sales Representative", new DateTime(1963, 07, 02), new DateTime(1993, 10, 17), 2, " 7 Houndstooth Rd.", " WG2 7LT", "(71) 555-4444 ", "London ", "UK "));
Emp.Add(new EmployeeView(7, "Robert", "King", "Sales Representative", new DateTime(1960, 05, 29), new DateTime(1994, 01, 02), 7, "Edgeham HollowWinchester Way ", "RG1 9SP ", "(71) 555-5598 ", "London ", " UK"));
Emp.Add(new EmployeeView(8, "Laura", "Callahan", "Inside Sales Coordinator", new DateTime(1958, 01, 09), new DateTime(1994, 03, 05), 9, "722 Moss Bay Blvd. ", "98033 ", " (206) 555-3412", "Seattle ", "USA "));
Emp.Add(new EmployeeView(9, "Anne", "Dodsworth", "Sales Representative", new DateTime(1966, 01, 27), new DateTime(1994, 11, 15), 5, "4726 - 11th Ave. N.E. ", "98105 ", "(71) 555-5598 ", " London", "UK "));
return Emp;
}
}
}
}
@* Program.cs *@
builder.Services.AddControllersWithViews().AddLumenUIJson();
builder.Services.AddLumenUI();
// ...
app.UseLumenUI();
@* Views/_ViewImports.cshtml *@
@using Lumen.UI.Mvc
@addTagHelper *, Lumen.UI.Core
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@await RenderSectionAsync("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The detail row template provides additional information about a data row which can be shown or hidden by clicking the expand or collapse button. The detailTemplate property accepts either the string or HTML element`s ID value, which will be used as the template for the detail row.
In this demo, Employees' information is displayed in the detail row.
Data Grid supports client-side exporting to export data to the Excel and PDF formats.
In this demo, the Employees' information can be exported using the exportDetailTemplate event where each cell can be customized. The ExcelExport and PdfExport items are defined in the toolbar of the Grid. Actions are defined in the toolbarClick event to export Grid data using excelExport and pdfExport methods with the hierarchyExportMode set to All.
The detail grids' export options can be changed by using the hierarchyExportMode property.
The detail template allows you to export the grid with the following options,
Expanded: Exports only the visible detail rows in an expanded state.All: Exports all the detail rows in an expanded state.None: Exports the detail rows in a collapsed state.