如何在不同页面中打印表格的所有内容?

时间:2019-04-02 09:58:16

标签: javascript google-app-maker

我要打印表的所有内容,但是如果表占用一个以上的页面,则仅在第一页显示该内容。

如何在不丢失页面绑定的情况下打印所有内容?

选项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();

1 个答案:

答案 0 :(得分:0)

问题确实很模糊,但是我从您的屏幕快照中得出的假设是,您希望有一个使用样式表将表内容打印出来的选项。

您可以为此使用document.writeprint()并添加链接到样式表的变量。可能需要进行调整,因为我不知道您的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>

这还将保留页面绑定。

相关问题