在不弄乱表格格式的情况下无法分页

soc*_*pet 11 html css sass reactjs

我一直遇到表格中的分页问题.以为我有一个解决方案,因为它在这个SO问题中工作得很好:

在React app中的<table>中插入分页符

这对于有一列的表来说很好,但是现在我正在处理多个列,这是一团糟.

基本上我必须包括display: block让分页符正常工作,但这使得它从格式良好的表转到此:

在此输入图像描述

我在MDN的列表中只是尝试了可能有用的任何东西.

https://developer.mozilla.org/en-US/docs/Web/CSS/display

此外,分页符仅在单独使用时才起作用,<tr>这是不合需要的,因为它会生成空白页.通过移动pagebreak<tr>而不是移动来解决这个问题<td>.

我无法解决这些问题; 关于如何解决这个问题的任何建议?

不确定JSFiddle在打印问题上有多大用处,但这里是编译后的HTML.我永远无法让JSFiddle使用React:

https://jsfiddle.net/5gz62d91/

最好的可能是Github回购:

https://github.com/ishraqiyun77/page-breaks

这是代码分开:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

import styles from '../assets/scss/app.scss';

class PageBreakIssues extends Component {

    // Render the data points
    renderDataPoint() {
        let dataPoint = [];
        for (let i = 1; i <= 3; i++) {
            let num = (Math.random() * 100).toFixed(2);
            dataPoint.push(
                <td className='data-point' key={ i }>
                    { num < 25 ? null : num }
                </td>
            )
        }
        return dataPoint;
    }

    // Start generating the row data
    renderDataRow() {
        let dataRow = [];
        for (let i = 1; i <= 5; i++) {
            dataRow.push(
                <tr key={ i }>
                    <td className='data-name' colSpan='3' key={i}>Test - { i }</td>
                    { this.renderDataPoint() }
                </tr>
            )
        }
        return dataRow;
    }

    // Start generating table sections with the section name
    // COMMENT THIS OUT TO TRY WITOUT ADDING A BLANK ROW
    renderSections() {
        let sections = [];
        for (let i = 1; i <= 10; i++) {
            sections.push(
                <tbody key={ i }>

                    <tr key={ i }>
                        <td colSpan='7' className='section-name' key={i} >
                            Section - { i }
                        </td>
                    </tr>
                    { this.renderDataRow() }
                    {
                        i % 2 === 0
                            ?
                            <tr className='pagebreak'>
                                <td colSpan='7'></td>
                            </tr>
                            :
                            null
                    }
                </tbody>
            )
        }   
        return sections;
    }

    // Start generating table sections with the section name
    // UNCOMMENT THIS SECTION TO TRY WITHOUT INSERT BLANK TR
    // renderSections() {
    //     let sections = [];
    //     for (let i = 1; i <= 10; i++) {
    //         sections.push(
    //             <tbody key={i}>
    //                 <tr key={i}>
    //                     <td colSpan='7' className={ i % 2 === 0? 'section-name pagebreak' : 'section-name'} key={i} >
    //                         Section - {i}
    //                     </td>
    //                 </tr>
    //                 {this.renderDataRow()}
    //             </tbody>
    //         )
    //     }
    //     return sections;
    // }

    // Render the table with <th>
    render() {
        return (
            <table>
                <thead>
                    <tr>
                        <th colSpan='3'>Results</th>
                        <th>1</th>
                        <th>2</th>
                        <th>3</th>
                    </tr>
                </thead>
                { this.renderSections() }
            </table>
        )
    }
}

ReactDOM.render(<PageBreakIssues />, document.getElementById('app'));
Run Code Online (Sandbox Code Playgroud)
@mixin borders {
    border: 1px solid black;
}

%borders {
    @include borders;
}

table {
    border-spacing: 0;

    th {
        text-align: center;
    }

    tr {
        th{
            @extend %borders;
        }

        td {
            @extend %borders;
            &.data-name {
                padding: 3px 100px 3px 3px;
            }

            &.data-point {
                text-align: center;
                padding: 3px 10px;
            }

            &.section-name {
                background-color: #999;
            }
        }
    }
}

@media print {
    tr {
        display: block;
    }

    .pagebreak {
        break-before: always !important;
        page-break-before: always !important;
        page-break-inside: avoid !important;
    }
}
Run Code Online (Sandbox Code Playgroud)

MT-*_*ong 10

我想出了一个更加硬编码的方法(所以调用完全解决了你的问题).我必须说它不优雅.

我的方法的主要思想是tbody改为display:block(像往常一样),但也添加.pagebreak到目标tbody.

但是,此方法tbody无法从表格中取消,因此不再与之对齐thead.这就是为什么我添加一个tr用于打印thead 的原因,并thead在打印时删除原点.

添加打印,不在普通视图中显示

//Thead part, add a printing thead only shown in Print
//As originaly thead will has alloction problem
{ i % 2 === 1 ?
  <tr className='printing-thead'>
    <td colspan="3">Results</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr> : null
}
....
...
//Corrected Page break
</tbody>
<tbody class="pagebreak">
    <tr>
        <td colspan="7"></td>
    </tr>
</tbody>
<tbody>
...
Run Code Online (Sandbox Code Playgroud)

对应的CSS

table {
  border-spacing: 0;
  table-layout: fixed;
  th {
    text-align: center;
  }
  tr {
    th {
      @extend %borders;
    }
    td {
      @extend %borders;
      &.data-name {
        padding: 3px 100px 3px 3px;
      }
      &.data-point {
        text-align: center;
        padding: 3px 10px;
      }
      &.section-name {
        background-color: #999;
      }
    }
  }
  tr.printing-thead {
    display: none;
  }
}

@media print {
  thead {
    display: none;
  }
  tbody {
    display: block;
    tr.printing-thead {
      display: contents;
      font-weight: bold;
      text-align: center;
    }
  }
  .pagebreak {
    height: 0px;
    break-before: always !important;
    page-break-before: always !important;
    page-break-inside: avoid !important;
    td {
      display: none;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

的jsfiddle.

反应版本

JSfiddle React版本

  • 为了实现MatrixTai解决方案,index.js的第42行变为`<tbody className = {i%2 === 0?'pagebreak':''} key = {i}>` (2认同)
  • 我测试了MatrixTai修复并在github上提交了他的解决方案:[https://github.com/xsimo/page-breaks/commit/8dd6c7974d2ef03ebf53a256dce05f2db23c0926] (2认同)