Search code examples
jqueryheadertablesorter

jQuery tablesorter change header on run-time


I've a question to jQuery tablesorter. Is there a way to change the headers-attribute on run-time?

On start I do this:

$(document).ready(function() {
        $("table").tablesorter({debug:false, headers: {4:{sorter:false}}}); 
});

In my Script there's a link:

<a href="javascript: xxx()">xxx</a>

This is calling this function:

function xxx() {
    $("table").tablesorter({debug:false, headers: {3:{sorter: false}}}); 
}

The goal is to disable the third column, too. Do you habe any ideas?


Solution

  • To toggle the sorting of a column, you need to do two things:

    • Toggle "sorter-false" class name on the header.
    • Toggle the sortDisabled attribute on the header.

    Here is a demo:

    $(function () {
        $('table').tablesorter({
            theme: 'blue'
        });
        $('button').click(function(){
            var $th = $('th:contains(Sex)'),
                status = !$th[0].sortDisabled;
            $th.toggleClass('sorter-false', status );
            $th[0].sortDisabled = status;
        });
    });