Search code examples
javascriptjqueryhtmltablesorter

Sort table but make one column not to move


I want to sort the table below but same time i don't want the numbering column to move, instead it should remain fixed while the other columns move. Any idea on how i should do it

$(document).ready(function() { 
    $("#myTable").tablesorter({ 
        // pass the headers argument and assing a object 
        headers: { 
            // assign the first column not to sort
          
           0: { 
                // disable it by setting the property sorter to false 
                sorter: false 
            }, 
        } 
    }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.21.2/js/jquery.tablesorter.min.js"></script>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.21.2/css/theme.blue.min.css' type='text/css' />
<table id='myTable' cellspacing="1" class="tablesorter-blue">             
    <thead>
        <tr> 
            <th>#</th> 
            <th>last name</th> 
            <th>age</th> 
            <th>total</th> 
            <th>discount</th> 
            <th>date</th> 
        </tr> 
    </thead> 
    <tbody> 
        <tr> 
            <td>1</td> 
            <td>parker</td> 
            <td>28</td> 
            <td>$9.99</td> 
            <td>20%</td> 
            <td>jul 6, 2006 8:14 am</td> 
        </tr> 
        <tr> 
            <td>2</td> 
            <td>hood</td> 
            <td>33</td> 
            <td>$19.99</td> 
            <td>25%</td> 
            <td>dec 10, 2002 5:14 am</td> 
        </tr> 
        <tr> 
            <td>3</td> 
            <td>kent</td> 
            <td>18</td> 
            <td>$15.89</td> 
            <td>44%</td> 
            <td>jan 12, 2003 11:14 am</td> 
        </tr> 
        <tr> 
            <td>4</td> 
            <td>almighty</td> 
            <td>45</td> 
            <td>$153.19</td> 
            <td>44%</td> 
            <td>jan 18, 2001 9:12 am</td> 
        </tr> 
        <tr> 
            <td>5</td> 
            <td>evans</td> 
            <td>22</td> 
            <td>$13.19</td> 
            <td>11%</td> 
            <td>jan 18, 2007 9:12 am</td> 
        </tr> 
    </tbody> 
</table>


Solution

  • You can bind to the sortEnd event and simply write new text to the first column.

    The following code takes advantage of the facts that

    • jQuery's .text() accepts a callback function to calculate and return the new text for each matched element (http://api.jquery.com/text/)
    • this callback function receives the (zero-based) element index as first argument, so that you don't need to keep a separate counter variable

    $(function() { 
        $("#myTable").tablesorter({ 
            headers: { 
                0: { 
                    sorter: false 
                }, 
            } 
        }).on("sortEnd", function () {
            $(this).find('tbody td:first-child').text(function (i) {
                return i + 1;
            });
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.21.2/js/jquery.tablesorter.min.js"></script>
    <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.21.2/css/theme.blue.min.css' type='text/css' />
    <table id='myTable' cellspacing="1" class="tablesorter-blue">             
        <thead>
            <tr> 
                <th>#</th> 
                <th>last name</th> 
                <th>age</th> 
                <th>total</th> 
                <th>discount</th> 
                <th>date</th> 
            </tr> 
        </thead> 
        <tbody> 
            <tr> 
                <td>1</td> 
                <td>parker</td> 
                <td>28</td> 
                <td>$9.99</td> 
                <td>20%</td> 
                <td>jul 6, 2006 8:14 am</td> 
            </tr> 
            <tr> 
                <td>2</td> 
                <td>hood</td> 
                <td>33</td> 
                <td>$19.99</td> 
                <td>25%</td> 
                <td>dec 10, 2002 5:14 am</td> 
            </tr> 
            <tr> 
                <td>3</td> 
                <td>kent</td> 
                <td>18</td> 
                <td>$15.89</td> 
                <td>44%</td> 
                <td>jan 12, 2003 11:14 am</td> 
            </tr> 
            <tr> 
                <td>4</td> 
                <td>almighty</td> 
                <td>45</td> 
                <td>$153.19</td> 
                <td>44%</td> 
                <td>jan 18, 2001 9:12 am</td> 
            </tr> 
            <tr> 
                <td>5</td> 
                <td>evans</td> 
                <td>22</td> 
                <td>$13.19</td> 
                <td>11%</td> 
                <td>jan 18, 2007 9:12 am</td> 
            </tr> 
        </tbody> 
    </table>