javascript - 如何在不同页面中打印所有表格的内容?
问题描述
我想打印一个表格的所有内容,但只显示第一页的内容,如果表格占据超过一页,则不打印所有内容。
如何在不丢失页面绑定的情况下打印所有内容?
选项1:
$(document).ready(function() {
$('.nonPrintable').hide();
window.print();
$('.nonPrintable').show();
});
选项 2(使用 printJS 库):
printJS({
printable: 'table',
type: 'html',
targetStyles: ['*'],
maxWidth: 1500
});
选项 3(这将打印所有没有 CSS 的内容):
var divElement = document.getElementById('table').innerHTML;
var printWindow = window.open("", "_blank", "");
printWindow.document.open();
printWindow.document.write('<html><body>');
printWindow.document.write(divElement);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.focus();
printWindow.print();
解决方案
问题真的很模糊,但是我从您的屏幕截图中假设您希望选择使用样式表打印出表格内容。
您可以为此使用document.write
and并添加链接到您的样式表的变量。print()
可能会进行调整,因为我不知道你的DOM
样子。
JS
$(function () {
$('#print').click(function () {
var pageTitle = 'Title of sheet here',
stylesheet = 'path/to/style.css',
win = window.open('', 'Print', 'width=990,height=590');
win.document.write('<html><head><title>' + pageTitle + '</title>' +
'<link rel="stylesheet" href="' + stylesheet + '">' +
'</head><body>' + $('.tableClass')[0].outerHTML + '</body></html>');
win.document.close();
win.print();
win.close();
return false;
});
});
然后只需按一下按钮即可打印内容。
HTML
<button type="button" id="print">Print here!</button>
这也将保留页面绑定。
推荐阅读
- regex - RegExp 匹配所有直到两个连续的特殊字符 (]])
- php - Laravel5.8:此路由不支持 GET 方法。支持的方法:POST。问题
- python - Python to_json 函数未将 DataFrame 转换为日期字段的正确格式的 Json
- java - 如何使用 JAVA 从动态创建的 JSON 中递归获取所有键值对
- c++ - 为什么我无法成功登录?
- java - 在区域设置“en_US”的代码“user.title”下未找到消息
- html - 是否有一个 HTML 标签的内容不能包含 JavaScript?
- ios - UIKit的textColor的iOS13白色问题
- c# - 在 C# 中将 JWT 令牌单一声明属性添加为数组元素
- c# - TPL 数据流反馈回路