printReport(): void {
let gridWindow = window.open(null, 'Print', 'toolbar=no, menubar=no, dialog=no, location=no, status=yes, resizable=yes, scrollbars=yes');
let htmlStart =
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<meta charset="utf-8" />' +
'<title>' + this.report + '</title>' +
'<style type="text/css" media="print">@page {margin: 0.38in 0.1in 0.4in 0.1in;}</style>' + // print margin
'<style type="text/css">table{width:100%;margin-bottom:1rem;background-color:transparent;border-collapse:collapse}table thead{background:#dee2e6}table thead th{vertical-align:bottom;padding:.2rem;border:none;border-left:.5px solid #c1c6cc;border-right:.5px solid #c1c6cc;text-align:center}table tbody tr td{border-left:.5px solid #dee2e6;border-right:.5px solid #dee2e6;padding:1px;vertical-align:top;text-align:center}table tbody tr:nth-child(odd){background:#fff}table tbody tr:nth-child(even){background:#f9fafb}th{text-align:inherit}</style>' +
'</head><body>';
let htmlEnd = '</body>' +
'</html>';
let html = htmlStart + this.dataToTableHtml() + htmlEnd;
gridWindow.document.write(html);
gridWindow.moveTo(0, 0); // move window to 0,0 coordinate and resize to window width, height
gridWindow.resizeTo(window.outerWidth, window.outerHeight);
gridWindow.onresize = function (e) {
gridWindow.focus(); // focus content after resize
};
gridWindow.onfocus = function (e) {
gridWindow.print(); // print content after focused
};
gridWindow.onafterprint = function (e) { // close window after print or cancel
gridWindow.close();
};
}
dataToTableHtml() { // generate html table
let headerComplete = false;
let headerLen = (this.columns.length - 1);
let tbody = '<tbody>';
let thead = '<thead><tr>';
this.gridData.forEach(d => {
this.columns.forEach((c, index) => {
// insert table header ... Inserted once for first loop of row
let hed = (c['display'] == '' || c['display'] == null) ? c['name'] : c['display'];
if (!headerComplete) {
if (index < headerLen) thead += '<th>' + hed + '</th>';
else { // (index == headerLen)
thead += '<th>' + hed + '</th></tr></thead>';
headerComplete = true;
}
}
// insert rows
let val = d[c['name']] ? d[c['name']] : '-';
if (index < headerLen) {
if (index == 0) tbody += '<tr><td>' + val + '</td>'; // first td of row = append tr start
else if (index == headerLen) tbody += '<td>' + val + '</td></tr>'; // last td of row = append tr end
else tbody += '<td>' + val + '</td>'; // middle td of row
}
else tbody += '<td>' + val + '</td></tr>'; // (index == headerLen)
});
});
tbody += '</tbody>';
return '<table>' + thead + tbody + '</table>';
}
Comments
Post a Comment