Pivot Table in ASP.NET Core:
Youtube Video Link: https://bit.ly/37Afvq1
1. Index.cshtml :==========================================================
@page
@model IndexModel
@{
ViewData["Title"] = "Home page with Pivot";
}
<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>
<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>
<div class="container">
<div>
<input type="button" class="btn-info" onclick="tableToExcel('pvtTable', 'PivotDownload')" value="Export to Excel">
</div>
<div id="pivotoutput" style="margin: 30px;"></div>
</div>
@*//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>
2. Index.cshtml.cs :
===============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace PivotTableCore.Pages
{
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);
}
}
}
3. Startup.cs :
===========================================================
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddMvc().AddJsonOptions(o =>//add
{
o.JsonSerializerOptions.PropertyNamingPolicy = null;
o.JsonSerializerOptions.DictionaryKeyPolicy = null;
});
services.Configure<CookiePolicyOptions>(options =>//add
{
// 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>();//add
}
4. Layout.cshtml :
==========================================================
<head>
<script src="~/lib/jquery/jquery.min.js"></script>
<script src="~/lib/jquery/jquery-ui.js"></script>
<script src="~/pivot/pivot.js"></script>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link href="~/pivot/pivot.css" rel="stylesheet" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
0 Comments