Pivot Table in ASP.NET Core
_Layout.cshtml :
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jqueryui/jquery-ui.min.js"></script>
<link href="~/pivot/pivot.min.css" rel="stylesheet" />
<script src="~/pivot/pivot.min.js"></script>
Index.cshtml:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">Invoice List</h1>
</div>
<table class="table table-sm">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.InvoiceList[0].InvoiceNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.InvoiceList[0].Amount)
</th>
<th>
@Html.DisplayNameFor(model => model.InvoiceList[0].CostCategory)
</th>
<th>
@Html.DisplayNameFor(model => model.InvoiceList[0].Period)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.InvoiceList)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.InvoiceNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Amount)
</td>
<td>
@Html.DisplayFor(modelItem => item.CostCategory)
</td>
<td>
@Html.DisplayFor(modelItem => item.Period)
</td>
<td></td>
</tr>
}
</tbody>
</table>
<div class="container">
<div>
<input type="button" class="btn btn-info" onclick="tableToExcel('pvtTable','PivotDownload')" value="Export to Excel" />
</div>
<div id="pivotoutput" style="margin:30px;"></div>
</div>
<script>
var myInvoiceDetails = [];
function drawPivot() {
$(function () {
$("#pivotoutput").pivotUI(
myInvoiceDetails,
{
rows: ["CostCategory"],
cols: ["Period"],
vals: ["Amount"],
aggregatorName: "Sum"
}
)
});
}
function getPivot() {
return fetch('./Index?handler=InvoicePivotData',
{
method: 'get',
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
})
.then(function (response) {
if (response.ok) {
return response.text();
} else {
throw Error("Response Not Ok");
}
})
.then(function (text) {
try {
return JSON.parse(text);
} catch (err) {
throw Error("Method Not Found");
}
})
.then(function (responseJSON) {
myInvoiceDetails = responseJSON;
drawPivot();
})
}
getPivot();
</script>
@*//For Generate Excel*@
<script type="text/javascript">
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
return function (table, name) {
if (!table.nodeType) table = document.getElementsByClassName(table)[0]
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
Index.cshtml.cs:
public class IndexModel : PageModel
{
private readonly InvoiceService _invoiceService;
public List<InvoiceModel> InvoiceList;
public IndexModel(InvoiceService invoiceService)
{
_invoiceService = invoiceService;
}
public void OnGet()
{
InvoiceList = _invoiceService.GetInvoices();
}
public JsonResult OnGetInvoicePivotData()
{
InvoiceList= _invoiceService.GetInvoices();
return new JsonResult(InvoiceList);
}
}
InvoiceModel.cs :
public class InvoiceModel
{
public int InvoiceNumber { get; set; }
public double Amount { get; set; }
public string CostCategory { get; set; }
public string Period { get; set; }
}
InvoiceService.cs :
public class InvoiceService
{
public List<InvoiceModel> GetInvoices()
{
return new List<InvoiceModel>()
{
new InvoiceModel(){ InvoiceNumber=1,Amount=20,CostCategory="Services",Period="2019_11"},
new InvoiceModel() {InvoiceNumber = 2, Amount = 60, CostCategory = "Telephone", Period="2019_12"},
new InvoiceModel() {InvoiceNumber = 3, Amount = 30, CostCategory = "Services", Period="2019_11"},
new InvoiceModel() {InvoiceNumber = 4, Amount = 50, CostCategory = "Consultancy", Period="2019_11"},
new InvoiceModel() {InvoiceNumber = 5, Amount = 60, CostCategory = "Raw materials", Period="2019_10"},
new InvoiceModel() {InvoiceNumber = 6, Amount = 10, CostCategory = "Raw materials", Period="2019_11"},
new InvoiceModel() {InvoiceNumber = 7, Amount = 40, CostCategory = "Raw materials", Period="2019_11"},
new InvoiceModel() {InvoiceNumber = 8, Amount = 30, CostCategory = "Utilities", Period="2019_11"},
};
}
}
Startup.cs :
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddMvc().AddJsonOptions(o =>
{
o.JsonSerializerOptions.PropertyNamingPolicy = null;
o.JsonSerializerOptions.DictionaryKeyPolicy = null;
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddTransient<InvoiceService>();
}
0 Comments