Serjical Kafka
Serjical Kafka

Reputation: 377

Rotate an html table 90 degrees in the anticlockwise direction

I want to rotate the entire table 90 degrees in the anticlockwise direction. ie. contents of td(row=ith,column=jth) should be transferred to the td(row = (total number of rows-j+1)th , column = ith). The text inside the div also should be rotated by 90degrees.

<table><tbody>
<tr><td>a</td><td>1</td><td>8</td></tr>
<tr><td>b</td><td>2</td><td>9</td></tr>
<tr><td>c</td><td>3</td><td>10</td></tr>
<tr><td>d</td><td>4</td><td>11</td></tr>
<tr><td>e</td><td>5</td><td>12</td></tr>
<tr><td>f</td><td>6</td><td>13</td></tr>
<tr><td>g</td><td>7</td><td>14</td></tr>
</tbody></table>   

this table should be transformed to

<table><tbody>
<tr>
    <td>8</td><td>9</td><td>10</td><td>11</td>
    <td>12</td><td>13</td><td>14</td>
</tr>
<tr>
    <td>1</td><td>2</td><td>3</td><td>4</td>
    <td>5</td><td>6</td><td>7</td>
</tr>
<tr>
    <td>a</td><td>b</td><td>c</td><td>d</td>
    <td>e</td><td>f</td><td>g</td>
</tr>
</tbody></table> 

I can do this by javascript loops. But its very long. I want to know whether there is a more elegant way. Thanks in advance.

Upvotes: 8

Views: 34488

Answers (3)

Pragnesh Chauhan
Pragnesh Chauhan

Reputation: 8476

try this DEMO

reference : Convert TD columns into TR rows and modified david answer

TransposeTable('myTable');
function TransposeTable(tableId)
{        
  var tbl = $('#' + tableId);
  var tbody = tbl.find('tbody');
  //var oldWidth = tbody.find('tr:first td').length;
  var oldWidth = 0;
   //calculate column length if there is 'td=span' added    
   $('tr:nth-child(1) td').each(function () {
      if ($(this).attr('colspan')) {
          oldWidth += +$(this).attr('colspan');
      } else {
          oldWidth++;
      }
  });
  var oldHeight = tbody.find('tr').length;
  var newWidth = oldHeight;
  var newHeight = oldWidth;

  var jqOldCells = tbody.find('td');

  var newTbody = $("<tbody></tbody>");
  for(var y=newHeight; y>=0; y--)
  {
      var newRow = $("<tr></tr>");
      for(var x=0; x<newWidth; x++)
      {
          newRow.append(jqOldCells.eq((oldWidth*x)+y));
      }
      newTbody.append(newRow);
  }

  tbl.addClass('rotate');

  tbody.replaceWith(newTbody);        
}  

as per Robbert suggestion CSS

.rotate {
  -moz-transform: rotate(-90.0deg);  /* FF3.5+ */
  -o-transform: rotate(-90.0deg);  /* Opera 10.5 */
  -webkit-transform: rotate(-90.0deg);  /* Saf3.1+, Chrome */
  filter:  progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083);  /* IE6,IE7 */
  -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083)"; /* IE8 */

  padding-right:50px;
}

Upvotes: 1

Alberto
Alberto

Reputation: 1863

Cheating by creating a already rotated table (trivial since you generate table from array). JS just to hide/show tables.

Check this: http://jsfiddle.net/nUvgp/

PRO: easy

CONS: duplicated table code

HTML:

<a href="#" id="rotate">Rotate!</a>
<table id='myTable'><tbody>
<tr><td>a</td><td>1</td><td>8</td></tr>
<tr><td>b</td><td>2</td><td>9</td></tr>
<tr><td>c</td><td>3</td><td>10</td></tr>
<tr><td>d</td><td>4</td><td>11</td></tr>
<tr><td>e</td><td>5</td><td>12</td></tr>
<tr><td>f</td><td>6</td><td>13</td></tr>
<tr><td>g</td><td>7</td><td>14</td></tr>
</tbody></table>
<table id='myRotatedTable'><tbody>
<tr>
    <td>8</td><td>9</td><td>10</td><td>11</td>
    <td>12</td><td>13</td><td>14</td>
</tr>
<tr>
    <td>1</td><td>2</td><td>3</td><td>4</td>
    <td>5</td><td>6</td><td>7</td>
</tr>
<tr>
    <td>a</td><td>b</td><td>c</td><td>d</td>
    <td>e</td><td>f</td><td>g</td>
</tr>
</tbody></table> 

CSS:

#myRotatedTable {
    display:none;
}
#myRotatedTable td {
    -webkit-transform: rotate(270deg);
    -moz-transform: rotate(270deg);
    -o-transform: rotate(270deg);
    writing-mode: lr-tb;
}

JS:

$('#rotate').click(function (e) {
    e.preventDefault();
    $("#myTable").toggle();
    $("#myRotatedTable").toggle();
})

Upvotes: 3

Spudley
Spudley

Reputation: 168655

The text inside the div also should be rotated by 90degrees.

So basically you just want the whole thing to be rotated as a block?

Maybe you should just use CSS?

#myTable {
    transform:rotate(270deg);
}

Upvotes: 9

Related Questions