Skip to content

Commit 87a3097

Browse files
author
Yousef Sultan
committed
Print with table styles
Signed-off-by: Yousef Sultan <[email protected]>
1 parent 869448e commit 87a3097

File tree

4 files changed

+61
-39
lines changed

4 files changed

+61
-39
lines changed

docs/css/app.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/js/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs_src/App.vue

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,6 @@
3232
><i class="material-icons">menu</i></a>
3333
</div>
3434
<ul id="nav-mobile" class="sidenav sidenav-fixed">
35-
<li class="logo">
36-
<img
37-
src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/95/Vue.js_Logo_2.svg/512px-Vue.js_Logo_2.svg.png"
38-
width="40px"
39-
height="40px"
40-
>
41-
</li>
42-
<!-- <li class="version">
43-
<a href="#" data-target="version-dropdown" class="dropdown-trigger">
44-
1.0.0
45-
</a>
46-
<ul id="version-dropdown" class="dropdown-content">
47-
<li><a>1.0.0</a></li>
48-
<li>
49-
<a href="http://archives.materializecss.com/0.100.2/">0.100.2</a>
50-
</li>
51-
</ul>
52-
</li> -->
53-
<!-- <li class="search">
54-
<div class="search-wrapper">
55-
<input id="search" placeholder="Search" /><i class="material-icons"
56-
>search</i
57-
>
58-
<div class="search-results"></div>
59-
</div>
60-
</li> -->
6135
<li class="bold">
6236
<a href="#basic" class="waves-effect waves-teal">Basic Example</a>
6337
</li>
@@ -76,14 +50,6 @@
7650
<li class="bold">
7751
<a href="#row-buttons" class="waves-effect waves-teal">Row buttons</a>
7852
</li>
79-
<!-- <li class="bold">
80-
<a href="themes.html" class="waves-effect waves-teal"
81-
>New FunFunc!<span
82-
data-badge-caption="updated"
83-
class="new badge"
84-
></span
85-
></a>
86-
</li> -->
8753
</ul>
8854
</header>
8955

src/DataTable.vue

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="card material-table">
2+
<div class="card material-table" ref="table">
33
<div class="table-header">
44
<span class="table-title">{{ title }}</span>
55
<div class="actions">
@@ -334,9 +334,26 @@
334334
},
335335
336336
print() {
337-
let win = window.open('');
338-
win.document.write(this.renderTable());
337+
const clonedTable = this.$refs.table.cloneNode(true);
338+
this.synchronizeCssStyles(this.$refs.table, clonedTable, true);
339+
340+
clonedTable.style.maxWidth = '100%';
341+
clonedTable.style.boxShadow = '0px 0px 0px 1px rgba(0,0,0,0.2)';
342+
clonedTable.style.margin = '8px auto';
343+
clonedTable.querySelector('.actions').remove();
344+
clonedTable.querySelector('.material-pagination').remove();
345+
clonedTable.querySelector('.datatable-length').remove();
346+
347+
clonedTable.querySelectorAll('button').forEach(n => n.remove());
348+
349+
let win = window.open('', '');
350+
351+
352+
win.document.body.style.fontFamily = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif';
353+
win.document.body.innerHTML = (clonedTable.outerHTML);
354+
339355
win.print();
356+
340357
win.close();
341358
},
342359
@@ -393,6 +410,45 @@
393410
else
394411
return undefined;
395412
},
413+
414+
/* https://codebottle.io/s/31b70f5391
415+
*
416+
* @author: Luigi D'Amico (www.8bitplatoon.com)
417+
*/
418+
synchronizeCssStyles(src, destination, recursively) {
419+
destination.style.cssText = this.getComputedStyleCssText(src);
420+
421+
if (recursively) {
422+
const vSrcElements = src.getElementsByTagName('*');
423+
const vDstElements = destination.getElementsByTagName('*');
424+
425+
for (var i = vSrcElements.length; i--;) {
426+
const vSrcElement = vSrcElements[i];
427+
const vDstElement = vDstElements[i];
428+
vDstElement.style.cssText = this.getComputedStyleCssText(vSrcElement);
429+
}
430+
}
431+
},
432+
433+
// https://gist.github.com/johnkpaul/1754808
434+
//
435+
// Please delete Firefox.
436+
getComputedStyleCssText(element) {
437+
const cssObject = window.getComputedStyle(element);
438+
const cssAccumulator = [];
439+
440+
if (cssObject.cssText !== ''){
441+
return cssObject.cssText;
442+
}
443+
444+
for (let prop in cssObject){
445+
if (typeof cssObject[prop] === 'string'){
446+
cssAccumulator.push(prop + ' : ' + cssObject[prop]);
447+
}
448+
}
449+
450+
return cssAccumulator.join('; ');
451+
},
396452
},
397453
398454
computed: {

0 commit comments

Comments
 (0)