Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move td to new column

I have a table like that:

<table>
  <tr>
    <td>some column|1</td>
    <td id="abc|1">abc</td>
  </tr>
  <tr>
    <td>another column|1</td>
    <td id="def|1">def</td>
  </tr>
  <tr>
    <td>some column|2</td>
    <td id="abc|2">abc</td>
  </tr>
  <tr>
    <td>another column|2</td>
    <td id="def|2">def</td>
  </tr>
</table>

How can I move the tds with the suffix |2 to the right, so adding a 3rd column? Also, the remaining "empty" td "some column|2" and "another column|2" should be removed completely.

The final result should look as follows:

enter image description here

This is the desired code:

<table>
  <tr>
    <td>some column|1</td>
    <td id="abc|1">abc</td>
    <td id="abc|2">abc</td>
  </tr>
  <tr>
    <td>another column|1</td>
    <td id="def|1">def</td>
    <td id="def|2">def</td>
  </tr>
</table>

This is my approach, which doesn't work:

$("table td:nth-child(2)[id$=2]").after("table td:nth-child(2)");

FIDDLE.

like image 481
Evgenij Reznik Avatar asked Jun 02 '16 14:06

Evgenij Reznik


People also ask

How set td width in HTML table?

Using width attribute: The <td> tag has width attribute to control the width of a particular column. By assigning a numeric value to this attribute between 0 to 100 in terms of percentage(or you can use pixel format). We can restrict the column width up to that much percentage of the table's total width.

What is TD in table?

<td>: The Table Data Cell element. The <td> HTML element defines a cell of a table that contains data. It participates in the table model.

How do you calculate TD value?

You can use the Core/index function in a given context, for example you can check the index of the TD in it's parent TR to get the column number, and you can check the TR index on the Table, to get the row number: $('td'). click(function(){ var col = $(this). parent().


2 Answers

Use this code

var firstTd = $("table").find("tr:nth-child(3) > td:nth-child(2)");
var secondTd = $("table").find("tr:nth-child(4) > td:nth-child(2)");

$("table").find("tr:nth-child(3), tr:nth-child(4)").remove();
$("table").find("tr:nth-child(1)").append(firstTd);
$("table").find("tr:nth-child(2)").append(secondTd);
table, tr, td {
    border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table>
  <tr>
    <td>some column|1</td>
    <td id="abc|1">abc</td>
  </tr>
  <tr>
    <td>another column|1</td>
    <td id="def|1">def</td>
  </tr>
  <tr>
    <td>some column|2</td>
    <td id="abc|2">abc</td>
  </tr>
  <tr>
    <td>another column|2</td>
    <td id="def|2">def</td>
  </tr>
</table>
like image 87
Mohammad Avatar answered Nov 15 '22 03:11

Mohammad


One way to achieve what you want:

// Values for new column to be added
var newVals = $("table td:nth-child(2)[id$=2]");

$("table td:nth-child(2)[id$=1]").each(function (ind) {
  if (ind < newVals.length) {
    // Get New Element to add
    var newElement = newVals[ind];
    // Remove original row for this element
    $(newElement).parent().remove();
    // Append to new column
    $(this).after($(newElement));                
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>some column|1</td>
    <td id="abc|1">abc</td>
  </tr>
  <tr>
    <td>another column|1</td>
    <td id="def|1">def</td>
  </tr>
  <tr>
    <td>ome column|2</td>
    <td id="abc|2">abc</td>
  </tr>
  <tr>
    <td>another column|2</td>
    <td id="def|2">def</td>
  </tr>
</table>
like image 39
Chitrang Avatar answered Nov 15 '22 04:11

Chitrang