Avoid/disable some specific rows from sorting process using jQuery tablesorter.js
Asked Answered
V

3

25

I have one table which I am sorting using jquery plugin tablesorter. Here i want to avoid first row {class="avoid-sort" } to be sort when any column is selected for sorting. example:


       <thead>
        <tr>
            <th class="header">#</th>
            <th class="header">Purchase Date</th>
            <th class="header">Course Name</th>
            <th class="header">Amount(in $)</th>
            <th class="header">User Name</th>
            <th class="header">Share</th>
            <th class="header">Net Revenue [$236.41]</th>
        </tr>
      </thead>
  <tbody>

       <tr class="avoid-sort">
            <th colspan="7">Total Revenue</th>
            <td>236.41</td>
        </tr>

        <tr>
                <td>1</td>
                <td>January 3rd, 2013</td>
                <td>Tackle Certification</td>
                <td>50</td>
                <td>Khushi Jha</td>
                <td>35</td>
                <td>33.69</td>
        </tr>
      <tr>
                <td>2</td>
                <td>January 3rd, 2013</td>
                <td>Flag Certification</td>
                <td>100</td> 
                <td>Pay</td>
                <td>70</td>
                 <td>67.67</td>
      </tr>
                            <tr>
                <td>3</td>
                <td>January 3rd, 2013</td>
                <td>Tackle Certification</td>
                <td>50</td>
                <!--                    <td>-->
                    <!--</td>-->
                <td>Pay</td>
                <td>35</td>
                 <td>33.69</td>

   </tr>


tr class="avoid-sort" should not come in sorting!

Please help!!

Virtual answered 9/1, 2013 at 8:31 Comment(1)
can't you use jQuery :not() ?Becker
T
36

You have two choices:

  1. If you are using the original tablesorter, you can get this static row widget to "lock" the row in place.

  2. If you are using my fork of tablesorter, you can just add a non-sortable tbody, like this (demo):

    <table>
    <thead>
      ...
    </thead>
    
    <!-- rows within this tbody are ignored -->
    <tbody class="avoid-sort">
      <tr>
        <th colspan="7">Total Revenue</th>
        <td>236.41</td>
      </tr>
    </tbody>
    
    <tbody>
      <!-- sortable rows -->
      <tr>
        ...
      </tr>
    </tbody>
    </table>
    

    then initialize the table like this:

    $(function() { 
    
      $("table").tablesorter({ 
        theme : 'blue', 
        cssInfoBlock : "avoid-sort", 
        widgets: [ 'zebra' ] 
      }); 
    
    });
    
Totalizator answered 10/1, 2013 at 6:18 Comment(2)
Top stuff, I had this problem and cssInfoBlock fixes the problem. Thanks!Interlay
You're welcome! My fork also as a version of the static row widget.Totalizator
S
2

In Mottie's fork the staticRow plugin is now built-in:

<script src="/js/tablesorter/jquery.tablesorter.min.js"></script>
<script src="/js/tablesorter/widgets/widget-staticRow.min.js"></script>
<!-- … -->
<tr class="static">…</tr>
$("table").tablesorter({
    widgets: ['staticRow']
})

Alternatively, you can set a custom class name:

<tr class="tablesorter-static">…</tr>
$("table").tablesorter({
    widgets: ['staticRow'],
    widgetOptions: {
        // Note it expects a CSS selector, not a raw class name
        staticRow_class: ".tablesorter-static"
    }
})
Superman answered 8/2, 2018 at 9:3 Comment(0)
C
-3
$(function() {     
$("#myTable").tablesorter({
        headers: {4: {sorter: false},8: {sorter: false}}
    });
});

here 4,8 are column numbers. column starts with 0

Ceram answered 15/11, 2016 at 12:24 Comment(1)
This is not what asked in the question. Your answer will disable sorting for columns with index 4 and 8. The question asked wants to ignore sorting certain rows, not columns. @ABDUL JAMALPitiful

© 2022 - 2024 — McMap. All rights reserved.