master
lynn 6 months ago
parent b56a0f4a76
commit f007ae4417

@ -1,12 +1,12 @@
<template>
<div>
<xy-dialog title="打印预览" :is-show.sync="isShow" :width="90" @on-ok="print" ok-text="">
<xy-dialog title="打印预览" :is-show.sync="isShow" :width="90" @on-ok="printHtml" ok-text="">
<template v-slot:normalContent>
<div class="form-switch">
<RadioGroup v-model="currentForm" type="button">
<Radio label="pre" :disabled="!getBeforeForms">事前审批表格</Radio>
<Radio label="post" :disabled="!getForms">事后支付表格</Radio>
<Radio label="finance" :disabled="!fundLog">财务审核表</Radio>
<Radio label="post" :disabled="!getForms">事后支付表格</Radio>
</RadioGroup>
</div>
@ -104,66 +104,76 @@ export default {
},
replaceControls(element) {
//
const inputs = element.getElementsByTagName('input')
const inputs = element.getElementsByTagName('input');
Array.from(inputs).forEach(input => {
const span = document.createElement('span')
let displayText = input.value || ''
//
const span = document.createElement('span');
let displayText = input.value || '';
if (input.type === 'checkbox' || input.type === 'radio') {
//
const checkedInput = element.querySelector(`input[name="${input.name}"]:checked`)
const checkedInput = element.querySelector(`input[name="${input.name}"]:checked`);
if (checkedInput) {
// label使label
const label = element.querySelector(`label[for="${checkedInput.id}"]`)
displayText = label ? label.textContent : checkedInput.value || ''
const label = element.querySelector(`label[for="${checkedInput.id}"]`);
displayText = label ? label.textContent : checkedInput.value || '';
} else {
displayText = ''
displayText = '';
}
} else if (input.type === 'date') {
displayText = input.value ? new Date(input.value).toLocaleDateString() : ''
displayText = input.value ? new Date(input.value).toLocaleDateString() : '';
}
span.textContent = displayText
//
span.style.cssText = window.getComputedStyle(input).cssText
span.style.display = 'inline-block'
span.style.width = input.offsetWidth + 'px'
span.style.height = input.offsetHeight + 'px'
span.style.border = 'none'
span.style.backgroundColor = 'transparent'
input.parentNode.replaceChild(span, input)
})
span.textContent = displayText;
//
const style = window.getComputedStyle(input);
span.style.cssText = style.cssText;
[
'width', 'height', 'padding', 'margin', 'font', 'fontSize', 'fontFamily',
'lineHeight', 'verticalAlign', 'border', 'background', 'color',
'boxSizing'
].forEach(key => {
span.style[key] = style[key];
});
// td
span.style.width = '100%';
span.style.display = 'block';
span.style.textAlign = 'center';
input.parentNode.replaceChild(span, input);
});
//
const selects = element.getElementsByTagName('select')
const selects = element.getElementsByTagName('select');
Array.from(selects).forEach(select => {
const span = document.createElement('span')
span.textContent = select.options[select.selectedIndex]?.text || ''
//
span.style.cssText = window.getComputedStyle(select).cssText
span.style.display = 'inline-block'
span.style.width = select.offsetWidth + 'px'
span.style.height = select.offsetHeight + 'px'
span.style.border = 'none'
span.style.backgroundColor = 'transparent'
select.parentNode.replaceChild(span, select)
})
const span = document.createElement('span');
span.textContent = select.options[select.selectedIndex]?.text || '';
const style = window.getComputedStyle(select);
span.style.cssText = style.cssText;
[
'width', 'height', 'padding', 'margin', 'font', 'fontSize', 'fontFamily',
'lineHeight', 'verticalAlign', 'border', 'background', 'color',
'boxSizing'
].forEach(key => {
span.style[key] = style[key];
});
span.style.width = '100%';
span.style.display = 'block';
span.style.textAlign = 'center';
select.parentNode.replaceChild(span, select);
});
//
const textareas = element.getElementsByTagName('textarea')
const textareas = element.getElementsByTagName('textarea');
Array.from(textareas).forEach(textarea => {
const span = document.createElement('span')
span.textContent = textarea.value || ''
//
span.style.cssText = window.getComputedStyle(textarea).cssText
span.style.display = 'inline-block'
span.style.width = textarea.offsetWidth + 'px'
span.style.height = textarea.offsetHeight + 'px'
span.style.border = 'none'
span.style.backgroundColor = 'transparent'
textarea.parentNode.replaceChild(span, textarea)
})
const span = document.createElement('span');
span.textContent = textarea.value || '';
const style = window.getComputedStyle(textarea);
span.style.cssText = style.cssText;
[
'width', 'height', 'padding', 'margin', 'font', 'fontSize', 'fontFamily',
'lineHeight', 'verticalAlign', 'border', 'background', 'color',
'boxSizing'
].forEach(key => {
span.style[key] = style[key];
});
span.style.width = '100%';
span.style.display = 'block';
span.style.textAlign = 'center';
textarea.parentNode.replaceChild(span, textarea);
});
},
async print() {
try {
@ -204,6 +214,37 @@ export default {
moneyFormat(val) {
if (!val && val !== 0) return '-'
return Number(val).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')
},
printHtml() {
//
const printNode = this.$refs.printtable.cloneNode(true);
//
this.replaceControls(printNode);
const win = window.open('', '_blank');
win.document.write(`
<html>
<head>
<title>打印</title>
<style>
@page { size: A4; margin: 10mm; }
body { margin: 0; padding: 0; }
.white-container, .form-container { width: 794px !important; margin: 0 auto; }
</style>
</head>
<body>
<div class="white-container">
<div class="form-container">
${printNode.innerHTML}
</div>
</div>
</body>
</html>
`);
win.document.close();
win.focus();
win.print();
win.close();
}
}
}

Loading…
Cancel
Save