2 years ago

#64882

test-img

check account

Remove duplicate tr from cloned variable

I have a table with two rows. I have cloned the entire table using clone(). When I export the table to Excel I see duplicate rows in the file because the second row contains a nested table. How can I remove the duplicate rows?

$("#jobExportExcel").click(function() {
  var table = $('#example').DataTable();
  var stable = document.createElement("table");
  var sthead = $(table.table().header()).clone(true)[0].outerHTML;
  var sbody = "";
  var row = $('#details').val();

  if (row == 'table') {
    table.$('tr').clone().each(function() { // right here i clone the rows 
      body += this.outerHTML;
    });
  } else {
    table.$('tr').clone().each(function() { // right here i clone the rows 
      body += this.outerHTML;
    });
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>sno</th>
      <th>name</th>
      <th>age</th>
      <th>education</th>
      <th>percentage</th>
      <th>branch name</th>
      <th> ph.no</th>
      <th> address</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td colspan="8">
        <table>
          <thead>
            <tr>
              <th></th>
              <th></th>
              <th></th>
              <th></th>
              <th></th>
            </tr>
            <tbody>
              <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
            </tbody>
        </table>
      </td>
    </tr>
  </tbody>
</table>

jquery

html-table

duplicates

clone

tablerow

0 Answers

Your Answer

Accepted video resources