I need to transpose an HTML table (swap rows and columns). I found numerous jQuery plugins but they are more than what I need.
I adapted some neat jQuery code from this stack but it does not work on tables that include thead and tfoot elements.
function tableTransform(objTable) {
objTable.each(function () {
var $this = $(this);
var newrows = [];
$this.find("tr").each(function () {
var i = 0;
$(this).find("td").each(function () {
i++;
if (newrows[i] === undefined) {
newrows[i] = $("<tr></tr>");
}
newrows[i].append($(this));
});
});
$this.find("tr").remove();
$.each(newrows, function () {
$this.append(this);
});
});
return false;
}
I created the fiddle below that provides an example of the markup and the code. Can someone update the function so it supports thead and tfoot elements?
http://jsfiddle.net/4tobvo05/4/
Just like the existing code, the new code must maintain the class and style values on each td as well as the table itself so the CSS is applied properly. It also needs to fixup the tfoot so it contains the correct number td cells that wrap a non-breaking space.