Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabling sort on all columns #205

Closed
tyilo opened this issue Dec 26, 2012 · 1 comment
Closed

Disabling sort on all columns #205

tyilo opened this issue Dec 26, 2012 · 1 comment

Comments

@tyilo
Copy link

tyilo commented Dec 26, 2012

Is there an easier way to do this than:

$("table").tablesorter({ 
  headers: {
    0: {sorter: false},
    1: {sorter: false},
     //...
    99: {sorter: false}
  }
});

If there indeed is a way of doing this, it is not documented or hard to find in the documentation.

@Mottie
Copy link
Owner

Mottie commented Dec 26, 2012

This question was also asked over on StackOverflow. Also check out this demo.

Basically, you can just set the sorter-false class in all desired header columns:

$('table thead th')
    .addClass('sorter-false')
    .filter(':eq(2), :eq(3)')
    // or select a range - the order of the filters is important!
    // .filter(':lt(10)').filter(':gt(3)') // allow sorting columns 4-9 (zero-based index)
    .removeClass('sorter-false');

The way the plugin checks the column sorter is in this order of priority:

  1. jQuery Data data-sorter="false" added to the th.
  2. Metadata class="{ sorter: false }". This requires the metadata plugin.
  3. Headers option headers : { 0 : { sorter: false } }.
  4. Header class name class="sorter-false".

So basically if a column has a class name of sorter-false and the headers options is { 0 : { sorter: 'digit' } }, the headers option wins.

There are other ways to disable column sorting. This method was also shown in that StackOverflow question because it would work with the original version of tablesorter:

var headers = {},
    $table = $('#myTable'), i,
    l = $table.find('thead th').length,

    // choose the columns you want to sort (zero-based index)
    sortcolumns = [2, 3, 4, 5];

// build headers object; based on sortcolumn selections
for (i = 0; i < l; i++) {
    if ($.inArray(i, sortcolumns) < 0) {
        headers[i] = { sorter: false }
    }
}
$table.tablesorter({
    widgets: ['zebra'],
    headers: headers // headers object built above
});

@Mottie Mottie closed this as completed Jan 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants