-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitTable.js
77 lines (65 loc) · 2.37 KB
/
splitTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @name Split Tables
* @description Splits Tables with the given class names
* @return {Node}
*/
function splitTables() {
splitTable("splitForPrint");
splitTable("SecondSplitting");
}
/**
* @name Split Table
* @description Splits Tables with the Class `className`
* @return {Node}
*/
function splitTable(className) {
if (!className) alert("Call splitTable with className of tables you want to spit")
console.log(`Splitting: "${className}"`);
/**
* @param {String} HTML representing any one element with any number of children
* @return {Node}
*/
function htmlToElement(html) {
var template = document.createElement("template");
template.innerHTML = html;
return template.content.firstChild;
}
const MaxHeight = 500;
var _RunningHeight = 0;
var _PageNo = 1;
// Save ordinal header for copying to the new tables
console.log(`Saving header`);
var thead = document.querySelector(`table.${className} > thead`);
console.log(`Setting saved header widths`);
thead.querySelectorAll(`table.${className} > thead > tr > th`).forEach((th) => (th.style.width = `${th.offsetWidth + 20}px`));
document.querySelectorAll(`table.${className} > tbody > tr`).forEach(function (e) {
if (_RunningHeight + e.offsetHeight > MaxHeight) {
console.log(`Detecting needed split(s): ${_PageNo}`);
_RunningHeight = 0;
_PageNo++;
}
_RunningHeight += e.offsetHeight;
e.setAttribute("data-page-no", _PageNo);
});
for (i = 1; i <= _PageNo; i++) {
console.log(`Creating new table: ${i}`);
document.querySelector(`table.${className}`).parentElement.appendChild(
htmlToElement(
`<div class="tablePage">
<table id="new_table${i}" class="${className}_child splitTable">
${thead.outerHTML}
<tbody></tbody>
</table>
</div>`
)
);
console.log(`Copy to new table : ${i}`);
var newTable = document.querySelector(`#new_table${i}.${className}_child > tbody`);
document.querySelectorAll(`table.${className} tr[data-page-no="${i}"]`).forEach((r) => newTable.appendChild(r));
}
console.log(`Removing old table: "${className}"`);
document.querySelector(`table.${className}`).remove();
console.log(`Done with splitting: "${className}"`);
}
// window.attachEvent("onload", splitTables);
document.addEventListener("DOMContentLoaded", splitTables);