Last active
July 11, 2017 10:10
-
-
Save ozrabal/de5457e66bdf694710fac19ddfc90e37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function splitTable(table, maxHeight) { | |
var header = table.children("thead"); | |
var footer = table.children("tfoot"); | |
if (!header.length) | |
return; | |
var headerHeight = header.outerHeight(); | |
var header = header.detach(); | |
var splitIndices = [0]; | |
var rows = table.children("tbody").children(); | |
maxHeight -= headerHeight; | |
var currHeight = 0; | |
rows.each(function(i, row) { | |
currHeight += $(rows[i]).outerHeight(); | |
if (currHeight > maxHeight) { | |
splitIndices.push(i); | |
currHeight = $(rows[i]).outerHeight(); | |
} | |
}); | |
splitIndices.push(undefined); | |
table = table.replaceWith('<div id="_split_table_wrapper"></div>'); | |
table.empty(); | |
for(var i=0; i<splitIndices.length-1; i++) { | |
var newTable = table.clone(); | |
header.clone().appendTo(newTable); | |
$('<tbody />').appendTo(newTable); | |
footer.clone().appendTo(newTable); | |
rows.slice(splitIndices[i], splitIndices[i+1]).appendTo(newTable.children('tbody')); | |
newTable.appendTo("#_split_table_wrapper"); | |
if (splitIndices[i+1] !== undefined) { | |
$('<div style="page-break-after: always; margin:0; padding:0; border: none;"></div>').appendTo("#_split_table_wrapper"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment