数据表打印复杂标题打印预览

时间:2019-03-22 07:13:22

标签: javascript html datatable

嗨,我正在使用datatable及其出色的功能,但是我在复杂的标头中遇到了问题

<thead>
    <tr><td>some text</td></tr>
    <tr><td>some text</td></tr>
</thead>

现在在显示页面中是这样的 enter image description here 当我点击打印预览时,我的搜索结果是这样的 enter image description here

tr中的第一个thead已消失 我打开了datatable.js文件,发现了

        var addRow = function ( d, tag ) {
        var str = '<tr>';

        for ( var i=0, ien=d.length ; i<ien ; i++ ) {
            // null and undefined aren't useful in the print output
            var dataOut = d[i] === null || d[i] === undefined ?
                '' :
                d[i];
            var classAttr = columnClasses[i] ?
                'class="'+columnClasses[i]+'"' :
                '';

            str += '<'+tag+' '+classAttr+'>'+dataOut+'</'+tag+'>';
        }

        return str + '</tr>';
    };

    // Construct a table for printing
    var html = '<table class="'+dt.table().node().className+'">';

    html += '<thead>';

    // Adding logo to the page (repeats for every page while print)
    if(config.repeatingHead.logo) {
        var logoPosition = (['left','right','center'].indexOf(config.repeatingHead.logoPosition) > 0) ? config.repeatingHead.logoPosition : 'right';
        html += '<tr><th colspan="'+data.header.length+'" style="padding: 0;margin: 0;text-align: '+logoPosition+';"><img style="'+config.repeatingHead.logoStyle+'" src="'+config.repeatingHead.logo+'"/></th></tr>';
    }

    // Adding title (repeats for every page while print)
    if(config.repeatingHead.title) {
        html += '<tr><th colspan="'+data.header.length+'">'+config.repeatingHead.title+'</th></tr>';
    }

    if ( config.header ) {
        html += addRow( data.header, 'th' );
    }

    html += '</thead>';

    html += '<tbody>';
    for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
        html += addRow( data.body[i], 'td' );
    }
    html += '</tbody>';

    if ( config.footer && data.footer ) {
        html += '<tfoot>'+ addRow( data.footer, 'th' ) +'</tfoot>';
    }
    html += '</table>';

及其仅在thead中添加最后一个tr 但我无法将第一张tr与印刷预览 非常感谢

这是一个jsfiddle ex 当您预览表格时,该表格在thead中带有拖行显示 但在打印预览中,它只能在广告中的tr上显示

enter link description here

2 个答案:

答案 0 :(得分:3)

thisDatatables website主题中所述,此功能尚不可用。

答案 1 :(得分:1)

您要求一种变通方法,并且由于它已经被提及,目前还不是Datatables的功能。您可以仅通过获取表格内容将其转换为PDF。然后为没有任何库的打印窗口添加样式。真的只是一个窗口。

只需要在加载窗口之前注入正确的样式即可。因此,我们只需要确保我们掌握了数据表输出的样式并将其注入即可。

enter image description here

  

CSS

    table
    {
        width: 300px;
        font-size: 17px;
    }
    table, th, td
    {
        border: solid 1px #DDD;
        border-collapse: collapse;
        padding: 2px 3px;
        text-align: center;
    }        table
    {
        width: 300px;
        font-size: 17px;
    }
    table, th, td
    {
        border: solid 1px #DDD;
        border-collapse: collapse;
        padding: 2px 3px;
        text-align: center;
    }
  

HTML

<div id="print-window">
    <table id="example" class="display" style="width:100%">
        <thead>
        <tr>
            <th rowspan="2">Name</th>
            <th colspan="2">HR Information</th>
            <th colspan="3">Contact</th>
        </tr>
        <tr>
            <th>Position</th>
            <th>Salary</th>
            <th>Office</th>
            <th>Extn.</th>
            <th>E-mail</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>$320,800</td>
            <td>Edinburgh</td>
            <td>5421</td>
            <td>t.nixon@datatables.net</td>
        </tr>
        <tr>
            <td>Garrett Winters</td>
            <td>Accountant</td>
            <td>$170,750</td>
            <td>Tokyo</td>
            <td>8422</td>
            <td>g.winters@datatables.net</td>
        </tr>
        <tr>
            <td>Ashton Cox</td>
            <td>Junior Technical Author</td>
            <td>$86,000</td>
            <td>San Francisco</td>
            <td>1562</td>
            <td>a.cox@datatables.net</td>
        </tr>
        <tr>
            <td>Cedric Kelly</td>
            <td>Senior Javascript Developer</td>
            <td>$433,060</td>
            <td>Edinburgh</td>
            <td>6224</td>
            <td>c.kelly@datatables.net</td>
        </tr>
        </tbody>
        <tfoot>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Salary</th>
            <th>Office</th>
            <th>Extn.</th>
            <th>E-mail</th>
        </tr>
        </tfoot>
    </table>
</div>
<input type="button" value="Print" onclick="pdf()" />
  

JavaScript

function pdf() {
    let t = document.getElementById('print-window').innerHTML;

    let style = "<style>";
    style = style + "table {width: 100%; font-size: 17px;}";
    style = style + "table, th, td {border: solid 1px #DDD; border-collapse: collapse;";
    style = style + "padding: 2px 3px;text-align: center;}";
    style = style + "</style>";
    let win = window.open('', '', 'height=700,width=700');

    win.document.write('<html><head>');
    win.document.write('<title>Profile</title>');
    win.document.write(style);
    win.document.write('</head>');
    win.document.write('<body>');
    win.document.write(t);
    win.document.write('</body></html>');

    win.document.close();
    win.print();
}