Search code examples
javascriptjquerysortingtablesorter

Applying the jQuery tablesort and re-numbering line item #'s


I need your help.

While I love this jQuery tablesorter plug-in, its only downfall is that my line numbers because out of sorts upon sorting another column. How would one renumber the line item #'s such that they would be in sequence. For example, if I were to click on the [First Name] column, the table becomes sorted as follows in the image below:

enter image description here

As you can clearly see, my numbers on the left are now out of sorts. How could delete and re-number the line item #'s in their proper sequence starting from (lowest to the highest integer).

Here is the markup in question:

$(document).ready(function() {
  $("#myTable").tablesorter();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://my-script-hosting.googlecode.com/files/jquery.tablesorter.min.js"></script>

<script type="text/javascript">
</script>

<table id="myTable" class="style1" border="1" cellspacing="1">
  <thead>
    <tr style="border-width: 1px; background-color: #C0C0C0">
      <th>#</th>
      <th>Last Name</th>
      <th>First Name</th>
      <th>Email</th>
      <th>Due</th>
      <th>Web Site</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Smith</td>
      <td>John</td>
      <td>jsmith@gmail.com</td>
      <td>$50.00</td>
      <td>http://www.jsmith.com</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Bach</td>
      <td>Frank</td>
      <td>fbach@yahoo.com</td>
      <td>$50.00</td>
      <td>http://www.frank.com</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Doe</td>
      <td>Jason</td>
      <td>jdoe@hotmail.com</td>
      <td>$100.00</td>
      <td>http://www.jdoe.com</td>
    </tr>
    <tr>
      <td>4</td>
      <td>Conway</td>
      <td>Tim</td>
      <td>tconway@earthlink.net</td>
      <td>$50.00</td>
      <td>http://www.timconway.com</td>
    </tr>
  </tbody>
</table>


Solution

  • After completing sorting it will fire sortEnd event at that time update the numbering

    $(document).ready(function() {
      $("#myTable").tablesorter().on("sortEnd", function() {
        $(this).find("tr:gt(0)").each(function(i) {
          $(this).find("td:eq(0)").text(i+1);         
        });
      })
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://my-script-hosting.googlecode.com/files/jquery.tablesorter.min.js"></script>
    
    <script type="text/javascript">
    </script>
    
    <table id="myTable" class="style1" border="1" cellspacing="1">
      <thead>
        <tr style="border-width: 1px; background-color: #C0C0C0">
          <th>#</th>
          <th>Last Name</th>
          <th>First Name</th>
          <th>Email</th>
          <th>Due</th>
          <th>Web Site</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Smith</td>
          <td>John</td>
          <td>jsmith@gmail.com</td>
          <td>$50.00</td>
          <td>http://www.jsmith.com</td>
        </tr>
        <tr>
          <td>2</td>
          <td>Bach</td>
          <td>Frank</td>
          <td>fbach@yahoo.com</td>
          <td>$50.00</td>
          <td>http://www.frank.com</td>
        </tr>
        <tr>
          <td>3</td>
          <td>Doe</td>
          <td>Jason</td>
          <td>jdoe@hotmail.com</td>
          <td>$100.00</td>
          <td>http://www.jdoe.com</td>
        </tr>
        <tr>
          <td>4</td>
          <td>Conway</td>
          <td>Tim</td>
          <td>tconway@earthlink.net</td>
          <td>$50.00</td>
          <td>http://www.timconway.com</td>
        </tr>
      </tbody>
    </table>