Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invert (transpose) the rows and columns of an HTML table?

I want to transpose the rows and columns in an HTML table, i.e. Rows as Columns, Columns as Rows.

In what way I can do it?

Example :

1 4 7   2 5 8   3 6 9 

as

1 2 3   4 5 6   7 8 9   
like image 883
Reddy Avatar asked Jun 09 '11 18:06

Reddy


People also ask

How do you reverse a table in HTML?

Get the array of tr s from tbody using . get() and use Array. reverse to reverse the elements and assign it back.

How do I split a column into a row in HTML?

You have two options. Use an extra column in the header, and use <colspan> in your header to stretch a cell for two or more columns. Insert a <table> with 2 columns inside the td you want extra columns in.


1 Answers

http://jsfiddle.net/CsgK9/2/

html:

<table>     <tr>         <td>1</td>         <td>4</td>         <td>7</td>     </tr>     <tr>         <td>2</td>         <td>5</td>         <td>8</td>     </tr>     <tr>         <td>3</td>         <td>6</td>         <td>9</td>     </tr> </table>   <p><a href="#">Do it.</a></p> 

js:

$("a").click(function(){     $("table").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; }); 
like image 92
Svante Svenson Avatar answered Oct 13 '22 14:10

Svante Svenson