jQuery tablesorter: How to disable sorting on a column by using a class instead of "inline JSON"?

Andrew picture Andrew · Jul 8, 2011 · Viewed 13.8k times · Source

I am using the jQuery tablesorter plugin. I know how to disable sorting on a column by using the jQuery Metadata plugin:

<th class="{sorter: false}">Don't sort me</th>

But I would rather do this by setting a class, so I don't have to use an additional plugin. Also I think I would remember the class name easier than remembering this JSON syntax. How can I do the same same thing using this syntax:

<th class="not-sortable">Don't sort me</th>

Answer

Eric Petroelje picture Eric Petroelje · Aug 20, 2011

You shouldn't have to modify the source of the plugin. Assuming your th class for not sorting is called nosort:

function setupTablesorter() {
    $('table.tablesorter').each(function (i, e) {
        var myHeaders = {}
        $(this).find('th.nosort').each(function (i, e) {
            myHeaders[$(this).index()] = { sorter: false };
        });

        $(this).tablesorter({ widgets: ['zebra'], headers: myHeaders });
    });    
}