Skip to content
This repository was archived by the owner on Oct 6, 2021. It is now read-only.

Commit 54dec55

Browse files
Simon MeadowsSimon Meadows
authored andcommitted
Re-Worked to allow for nested tables
1 parent 0d20d11 commit 54dec55

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

src/jquery.columnizer.js

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,67 @@
6464
};
6565
options = $.extend(defaults, options);
6666

67+
// Variable array for holding <thead> and <tfoot> from each table
68+
// As we split a table we need to keep copies of the <thead> and <tfoot> to place on each column
69+
var tables = new Array();
70+
71+
// Find all the table elements in the page
72+
$('table').each(function () {
73+
// Check if we have not already saved the <thead> and <tfoot>
74+
if (!$(this).hasClass('tableSaved')) {
75+
// Mark the found table by adding the .tableSaved class
76+
$(this).addClass('tableSaved')
77+
// Give the table a unique ID so we can re-add its elements later
78+
$(this).addClass('tableID-' + tables.length)
79+
// Save the tables unique ID, <thead>, and <tfoot> as an object in our tables array
80+
tables.push({
81+
tableID: 'tableID-' + tables.length,
82+
thead: $(this).find('thead:first').clone(),
83+
tfoot: $(this).find('tfoot:first').clone()
84+
});
85+
}
86+
});
87+
88+
// Function to add <thead> and <tfoot> to all tables in $pullOutHere
89+
// This function should be called anywhere split() returns as
90+
// that is the point where a column is complete and the remaining content
91+
// is not going to change until the next columnize() is called
92+
function fixTables($pullOutHere) {
93+
// Iterate through all of our saved tables
94+
for (i = 0; i < tables.length; i++) {
95+
// Check if the root element is a table
96+
if ($pullOutHere.is("table")) {
97+
// Check if the root element has any <tfoot> elements and
98+
// is the current table id for this loop
99+
if ($pullOutHere.children('tfoot').length == 0 &&
100+
$pullOutHere.hasClass(tables[i].tableID)) {
101+
// Add the <tfoot> to the table
102+
$(tables[i].tfoot).clone().prependTo($pullOutHere);
103+
}
104+
// Check if the root element has any <tfoot> elements and
105+
// is the current table id for this loop
106+
if ($pullOutHere.children('thead').length == 0 &&
107+
$pullOutHere.hasClass(tables[i].tableID)) {
108+
// Add the <tfoot> to the table
109+
$(tables[i].thead).clone().prependTo($pullOutHere);
110+
}
111+
}
112+
// Check if there are any child tables to the root element with the current table ID
113+
$pullOutHere.find('table .' + tables[i].tableID).each(function () {
114+
// Check if the child table has no <tfoot>
115+
if ($(this).children('tfoot').length == 0) {
116+
// Add the <tfoot> to the table
117+
$(tables[i].tfoot).clone().prependTo(this);
118+
}
119+
// Check if the child table has no <thead>
120+
if ($(this).children('thead').length == 0) {
121+
// Add the <thead> to the table
122+
$(tables[i].thead).clone().prependTo(this);
123+
}
124+
});
125+
}
126+
}
127+
67128
if(typeof(options.width) == "string"){
68129
options.width = parseInt(options.width,10);
69130
if(isNaN(options.width)){
@@ -187,11 +248,6 @@
187248
*/
188249
function columnize($putInHere, $pullOutHere, $parentColumn, targetHeight){
189250

190-
// Variables for dealing with <thead> and <tfoot> when splitting tables
191-
// As we split a table we need to keep copies of the <thead> and <tfoot> to place on each column
192-
var $thead;
193-
var $tfoot;
194-
195251
//
196252
// add as many nodes to the column as we can,
197253
// but stop once our height is too tall
@@ -300,11 +356,15 @@
300356
*/
301357
function split($putInHere, $pullOutHere, $parentColumn, targetHeight){
302358
if($putInHere.contents(":last").find(prefixTheClassName("columnbreak", true)).length){
359+
// Fix any tables that have had their <thead> and <tfoot> moved
360+
fixTables($pullOutHere);
303361
//
304362
// our column is on a column break, so just end here
305363
return;
306364
}
307365
if($putInHere.contents(":last").hasClass(prefixTheClassName("columnbreak"))){
366+
// Fix any tables that have had their <thead> and <tfoot> moved
367+
fixTables($pullOutHere);
308368
//
309369
// our column is on a column break, so just end here
310370
return;
@@ -395,17 +455,8 @@
395455
}
396456
}
397457
}
398-
// If we are in the process of splitting a table, add the <thead> and <tfoot>
399-
// clones back to $pullOutHere so they are available to move into the next column
400-
if($pullOutHere.prop('tagName') == 'TABLE'){
401-
if (this.$thead){
402-
$pullOutHere.prepend(this.$tfoot);
403-
}
404-
if (this.$thead) {
405-
$pullOutHere.prepend(this.$thead);
406-
}
407-
}
408-
458+
// Fix any tables that have had their <thead> and <tfoot> moved
459+
fixTables($pullOutHere);
409460
}
410461

411462

0 commit comments

Comments
 (0)