3

In an HTML document built up by a template engine, I have a table of accounting data -- a variable number of lines, with a "grand total" at the bottom. Sometimes this table spills over onto another page. When it does so, it's not obvious to the user that there's more data on another page, unless he's looking for the total and can't find it.

So, I put the grand total line in a TFOOT, so it would be printed on each page. Even if the user misses some of the line items, he can see the total right on the first page of the report.

What I'd like to do instead of printing the total is to print something like "(continued on next page)" at the bottom of the first page (where the TFOOT is dynamically inserted by the browser) and have the grand-total line print only on the last page that the table spans.

Is it possible to use HTML, CSS, or Javascript, to insert a "(continued...)" message at the end of every page that a multi-page table spans?

workerjoe
  • 2,421
  • 1
  • 26
  • 49
  • Just checking: when you say the table spills onto another page, what do you mean by "page" precisely? Do you mean the table has a set number of visible rows and a pagination system? – Stephen M Irving Jan 07 '20 at 14:46

4 Answers4

1

You could start by creating a media query containing your print styles and using content to add the "(continued...)" message.

@media print {
  tfoot::after {
    content: " continued...";
  }
}

Of course, the difficult part is to remove it on the last page. You may be able to accomplish that using Prince. Specifically, check the Two-Pass Solution.

thingEvery
  • 3,368
  • 1
  • 19
  • 25
1

No, to my knowledge it is not possible, at least not with CSS only.

Intuitively, a solution could be something of the sort:

@media print {
  tfoot:not(:last-child) span::after {
    content: "\2014 continued\2026";
  }
}
<table>
  <tfoot>
    <tr>
      <th><span>Footer</span></th>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <td><span>A</span></td>
    </tr>
  </tbody>
</table>

But even though when printing a HTML page containing a <table> that is too long to fit on one single page it is spread over multiple pages and the <tfoot> (and/or <thead>) is repeated on each page. There is still just one actual <tfoot> element in the HTML document and as far as I know CSS doesn't allow to select specific occurrences. And thus it is only possible to modify all of the repetitions (instead of for example all but the last one in our case).

sinoroc
  • 18,409
  • 2
  • 39
  • 70
0

It is possible if you are aware of data-tables. Data tables is a jquery plugin to your page which will insert pagination and total amount of pages your template/table contains. It'll be helpful to users to get proper idea about how many records you have. and at last row using colspan, you can always show the total. Rather than inserting 'Continue..', you will be able to show the count of total records.

For your reference check the data tables documentation: https://datatables.net/

Dimple
  • 11
  • 3
-1

Have you tried to use onbeforeprint? According to MDN:

...These events are raised before the print dialog window is opened... allow pages to change their content before printing starts...

So something general like this should work:

window.onbeforeprint = (event) => {
  const footers = document.querySelectorAll('tfoot');
  for (var i=0; i < footers.length; i++) {
    footers[i].innerHTML = 'continue...';
  }
  document.querySelector('table:last-of-type tfoot').innerHTML = 'last one';  
};
<h1>Hit conrtol+p or any other method for printing</h1>
<table><tfoot></tfoot></table>
<table><tfoot></tfoot></table>
<table><tfoot></tfoot></table>
<table><tfoot></tfoot></table>
<table><tfoot></tfoot></table>
<table><tfoot></tfoot></table>
<table><tfoot></tfoot></table>
<table><tfoot></tfoot></table>
A. Meshu
  • 4,053
  • 2
  • 20
  • 34
  • Can the downvoter explain why? seems that it actually find each tfoot as OP asked, change the content (with innerHTML that can switched to any other method) and change the last one to something else (OP didn't put his code so i put some general string). – A. Meshu Jan 20 '20 at 13:19
  • 1
    The original question is about one long table spreading over multiple pages, so that there is one single `` displayed multiple times. This answer is based on multiple tables. – sinoroc Jan 21 '20 at 09:24
  • @sinoroc i thought OP say that he got on every page `tfoot` that display the same string ("total... etc) and he want to change it to 'continue'... – A. Meshu Jan 21 '20 at 09:58
  • 1
    Yes that's a thing that happens automatically when a HTML page is printed. If a single `` spreads over multiple pages, then the `` and `` are repeated on each page. So there's just one of each in the HTML document, but they appear multiple times. So as far as I know CSS doesn't allow to select specific occurrences of those, and thus it is only possible to modify all of the repetitions (instead of all but the last one in our case).
    – sinoroc Jan 21 '20 at 10:16