My vue component like this :
JavaScript
x
27
27
1
<template>
2
3
<b-modal id="modalInvoice" size="lg" title="Invoice">
4
<Invoice/>
5
<div slot="modal-footer" class="w-100">
6
<b-btn size="sm" class="float-right" variant="warning" @click="show=false">
7
<i class="fa fa-print"></i> Print
8
</b-btn>
9
</div>
10
</b-modal>
11
12
13
<b-btn variant="warning" class="btn-square mt-2" v-b-modal.modalInvoice @click="checkout()"><i class="fa fa-credit-card-alt"></i>Checkout</b-btn>
14
15
</template>
16
<script>
17
18
export default {
19
20
methods: {
21
print() {
22
window.print()
23
}
24
}
25
}
26
</script>
27
If I click print button, it works. But it displays the entire website. I just want if I click the print button, it only shows the contents of the modal
How can I do it?
Advertisement
Answer
One way to doing this is by creating a clone of the modal and using css to limit visibility of what’s printed:
JavaScript
1
36
36
1
print () {
2
const modal = document.getElementById("modalInvoice")
3
const cloned = modal.cloneNode(true)
4
let section = document.getElementById("print")
5
6
if (!section) {
7
section = document.createElement("div")
8
section.id = "print"
9
document.body.appendChild(section)
10
}
11
12
section.innerHTML = "";
13
section.appendChild(cloned);
14
window.print();
15
}
16
17
@media screen {
18
#print {
19
display: none;
20
}
21
}
22
23
@media print {
24
body * {
25
visibility:hidden;
26
}
27
#print, #print * {
28
visibility:visible;
29
}
30
#print {
31
position:absolute;
32
left:0;
33
top:0;
34
}
35
}
36