Jquery copy first column of table to each next column

Viewed 72

I need for mobile view som manipulation of table. My table for desktop look like:

    <table>
                <thead>
                    <tr>
                        <th scope="row">&nbsp;</th>
                        <th scope="col">&nbsp;</th>
                        <th scope="col"><strong>Special Header</strong></th>
                        <th scope="col">&nbsp;</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th scope="row"><strong>First</strong></th>
                        <td><strong>Second</strong></td>
                        <td><strong>Third</strong></td>
                        <td><strong>fourth</strong></td>
                    </tr>
                    <tr>
                        <th scope="row"><strong>Text</strong></th>
                        <td>Text</td>
                        <td>Text</td>
                        <td>Text</td>
                    </tr>
                </tbody>
            </table>

For mobile devices I need to copy first colum to all other columns. Table should look like:

<table>
                <thead>
                    <tr>
                        <th scope="row">&nbsp;</th>
                        <th scope="col">&nbsp;</th>
                        <th scope="row">&nbsp;</th>
                        <th scope="col"><strong>Special Header</strong></th>
                        <th scope="row">&nbsp;</th>
                        <th scope="col">&nbsp;</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th scope="row"><strong>First</strong></th>
                        <td><strong>Second</strong></td>
                        <th scope="row"><strong>First</strong></th>
                        <td><strong>Third</strong></td>
                        <th scope="row"><strong>First</strong></th>
                        <td><strong>fourth</strong></td>
                    </tr>
                    <tr>
                        <th scope="row"><strong>Text</strong></th>
                        <td>Text</td>
                        <th scope="row"><strong>Text</strong></th>
                        <td>Text</td>
                        <th scope="row"><strong>Text</strong></th>
                        <td>Text</td>
                    </tr>
                </tbody>
            </table>

I found some Jquery, but this code only append first column at the end:

$('#addcolumn').click(function() {
    
    $('table tr').each(function() {
      $(this).append($(this).find('th:first').clone());
    });

  });

Can you please help to make Jquery code working for my problem, thank! Martin

1 Answers

You can use insertAfter this will insert element in the set of matched elements after the target .So, for tbody > td you can loop through tr and get first th cloned it then you can check if td is not last-child of tr if yes just add the cloned element after that td.Now , for thead you can simply use eq(0) to get first th and then use insertAfter to add the cloned after every th where scope="col".

Demo Code :

$('#addcolumn').click(function() {
  ///loop through tds
  $('tbody  td').each(function() {
    //get th
    var clone = $(this).closest("tr").find('th:first').clone()
    //if td is not last
    if (!$(this).is(':last-child')) {
      $(clone).insertAfter(this); //insert the cloned th
    }
  });
  //get the header first th
  var headers = $("table").find("thead th:eq(0)").clone();
  //insert cloned to th where scope=col and not last child
  $(headers).insertAfter("thead th[scope=col]:not(:last-child)");


});
$('#normal').click(function() {
//remove th not first child from thead and tbody
$("thead th[scope=row]:not(:first-child)").remove();
$("tbody th[scope=row]:not(:first-child)").remove();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th scope="row">&nbsp;</th>
      <th scope="col">&nbsp;</th>
      <th scope="col"><strong>Special Header</strong></th>
      <th scope="col">&nbsp;</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row"><strong>First</strong></th>
      <td><strong>Second</strong></td>
      <td><strong>Third</strong></td>
      <td><strong>fourth</strong></td>
    </tr>
    <tr>
      <th scope="row"><strong>Text</strong></th>
      <td>Text</td>
      <td>Text</td>
      <td>Text</td>
    </tr>
  </tbody>
</table>


<button id="addcolumn">Add</button>
<button id="normal">Reset</button>

Related