1

i have a html table with 10 column (i.e 1 2 3 4 5 6 7 8 9 10 ) and i want to flip columns hortizontally (i.e 10 9 8 7 6 5 4 3 2 1 ) using Jquery...

i have below code for this but this code is very long can anyone provide short code for this..

$(function() {
    jQuery.each($("table tr"), function() { 
        $(this).children(":eq(9)").after($(this).children(":eq(0)"));
        $(this).children(":eq(8)").after($(this).children(":eq(0)"));
        $(this).children(":eq(7)").after($(this).children(":eq(0)"));
        $(this).children(":eq(6)").after($(this).children(":eq(0)"));
        $(this).children(":eq(5)").after($(this).children(":eq(0)"));
        $(this).children(":eq(4)").after($(this).children(":eq(0)"));
        $(this).children(":eq(3)").after($(this).children(":eq(0)"));
        $(this).children(":eq(2)").after($(this).children(":eq(0)"));
        $(this).children(":eq(1)").after($(this).children(":eq(0)"));
    });
});

3 Answers 3

5

To achieve this you could build an array from the td elements within each row, reverse() it, then append them back again:

$('table tr').each(function() {
  var tds = $(this).children('td').get().reverse();
  $(this).append(tds);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Rory for your Quick reply but there is a problem i have another column and i want to fix this Column because this column have statement and i don't want to flip that column. html table with 11 column (i.e 1 2 3 4 5 6 7 8 9 10 Header) and i want to flip columns horizontally (i.e 10 9 8 7 6 5 4 3 2 1 Header) using Jquery... thanks in advance –
2

Loop through tds and in loop use .prepend() to inserting element in first of parent.

$("table td").each(function() { 
  $(this).parent().prepend(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
  </tr>
</table>

Update:

@NoumanSaeed: html table with 11 column (i.e 1 2 3 4 5 6 7 8 9 10 Header) and i want to flip columns horizontally (i.e 10 9 8 7 6 5 4 3 2 1 Header)

In this case you should exclude last td contain header in selector using :lt().

$("table td:lt(10)").each(function() { 
    $(this).parent().prepend(this);
});

$("table td:lt(10)").each(function() { 
  $(this).parent().prepend(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>Header</td>
  </tr>
</table>

7 Comments

@Thanks Mohammad for your Quick reply but there is a problem i have another column and i want to fix this Column because this column have statement and i don't want to flip that column. html table with 11 column (i.e 1 2 3 4 5 6 7 8 9 10 Header) and i want to flip columns horizontally (i.e 10 9 8 7 6 5 4 3 2 1 Header) using Jquery... thanks in advance
@NoumanSaeed Use $("table td:lt(10)") as selector. Check jsfiddle.net/ecuqzhd8
i am very thankful to you but i observe your above code did not reverse the complete table it only reverse first row, but i want to reverse complete table
i am very thankful to you but i observe your above code did not reverse the complete table it only reverse first row, but i want to reverse complete table
you are right, it did not working for me..but below mention code work for me but in this code header column is not fix as you fix in above code could you please do this for me.... Thanks in advance.. code is <script> $(document).ready(function(){ $('table tr').each(function() { var tds = $(this).children('td').get().reverse(); $(this).append(tds); }); }); </script>
|
0

The answers above are correct but you can also do with css.

NOTE Solution is not for table with pagination but if the data is short (one page only) you can give it a try!

if($("html").attr("dir") == "rtl") {
  $("table").css({"direction": "ltr"});
}else{
  $("table").css({"direction": "rtl"});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
  </tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.