我在主题Print subtotals on each page with HTML/CSS/JS maybe XSLT?中找到了一个代码(如下所示)。 我想在每个页面上添加标题表。 你能让我知道如何正确地做吗? 先感谢您。
<script>
function printSubtotals(table, columns) {
var
tbody = table.tBodies[0],
row = tbody.rows[0];
if(!row)
return;
var cellCount = row.cells.length;
if(!cellCount)
return;
var
subtotals = [],
rows = table.rows,
thead = table.tHead,
caption = table.querySelector('caption'),
colgroup = table.querySelector('colgroup'),
emptyTable = table.cloneNode(false),
emptyRow = row.cloneNode(true),
printDiv = document.createElement('div'),
overlap = document.createElement('div'),
subtotalCount = columns.length,
rowCount = rows.length - 1,
cells, subtotalCells, i, r;
if(colgroup && colgroup.parentNode === table)
emptyTable.appendChild(colgroup.cloneNode(true));
emptyTable.appendChild(tbody.cloneNode(false));
printDiv.className = /MSIE /.test(navigator.userAgent) ? 'print fixIE' : 'print';
overlap.className = 'overlap';
for(i = subtotalCount; i--; subtotals.push(0));
for(i = cellCount; i--; emptyRow.cells[i].innerHTML = '');
for(r = row.rowIndex; r < rowCount; r++) {
printDiv.appendChild(overlap.cloneNode(true));
tbody = printDiv.appendChild(emptyTable.cloneNode(true)).tBodies[0];
cells = tbody.appendChild(rows[r].cloneNode(true)).cells;
subtotalCells = tbody.appendChild(emptyRow.cloneNode(true)).cells;
for(i = subtotalCount; i--;) {
subtotals[i] += parseFloat(cells[columns[i]].innerHTML);
subtotalCells[columns[i]].innerHTML = '<b>Total: ' + subtotals[i] + '</b>';
}
}
printDiv.removeChild(printDiv.children[0]);
tbody = printDiv.children[0].tBodies[0];
if(caption && caption.parentNode === table)
tbody.parentNode.insertBefore(caption.cloneNode(true), tbody);
if(thead)
tbody.parentNode.insertBefore(thead.cloneNode(true), tbody);
table.parentNode.insertBefore(printDiv, table);
}
printSubtotals(document.querySelector('.data'), [0,1]);
</script>