Print subtotals on each page with HTML/CSS/JS maybe XSLT?

后端 未结 1 1293
清歌不尽
清歌不尽 2020-12-20 00:11

I would like to use the open standards for printing reports, this report would need to have subtotals on the end of each page.

The thread Use of XSL-FO, CSS3 instead

1条回答
  •  醉梦人生
    2020-12-20 00:48

    This can be done with plain old JavaScript, as demonstrated by the code below.

    
    
      
        
    MY TABLE
    Col 1Col 2
    11
    099
    11
    099
    11
    099
    29
    288
    20
    288
    20
    288
    31
    477
    31
    477
    31
    477
    48
    666
    41
    666
    41
    666
    51
    855
    51
    855
    51
    855
    67
    044
    62
    044
    62
    044
    71
    233
    71
    233
    71
    233
    86
    422
    83
    422
    83
    422
    91
    611
    91
    611
    91
    611
    10
    099
    14
    099
    14
    099
    21
    288
    21
    288
    21
    288
    31
    477
    31
    477
    31
    477
    45
    666
    45
    666
    45
    666
    51
    855
    51
    855
    51
    855
    64
    044
    66
    044
    66
    044
    71
    233
    71
    233
    71
    233
    83
    422
    87
    422
    87
    422
    91
    611
    91
    611
    91
    611
    12
    099
    18
    099
    18
    099
    21
    288
    21
    288
    21
    288
    30
    477
    39
    477
    39
    477
    41
    666
    41
    666
    41
    666
    51
    855
    50
    855
    50
    855
    61
    044
    61
    044
    61
    044
    70
    233
    71
    233
    71
    233
    81
    422
    81
    422
    81
    422
    90
    611
    92
    611
    92
    611

    What the Javascript function does is create a print-only version of the table in which every row has a hidden subtotals row attached to it. The subtotals row is covered up by the next row, so it's only visible if the next row gets bumped to the next page, or it's the last row in the table. This should work in just about any browser regardless of paper size. It may seem inefficient, but the perceived performance hit is negligible since the print table doesn't render on page load. Still, I wouldn't recommend this technique for tables with many thousands of rows.

    Note that the above code is just a proof-of-concept, and as such, is not particularly robust. A few things that I know will break it are:

    • empty cells in data columns
    • non-numeric values in data columns
    • multi-line content in tbody cells
    • varying cell count in tbody rows (e.g. some have colspans and others don't)
    • letting cell content determine column width
    • changing line-height or border-width without changing other, related CSS values

    Some of these things are doable with the right modifications to the JavaScript and CSS.

    0 讨论(0)
提交回复
热议问题