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

Disable sorting when table has less than X rows #209

Closed
Mottie opened this issue Jan 4, 2013 · 3 comments
Closed

Disable sorting when table has less than X rows #209

Mottie opened this issue Jan 4, 2013 · 3 comments

Comments

@Mottie
Copy link
Owner

Mottie commented Jan 4, 2013

I received a request to disable sorting when a minimum number of rows is not present. Also in that discussion was a request to allow hiding the thead with an empty table and modifying the table caption.

I think the best solution would be to use the initialized and updateComplete callback functions, but I won't be able to make a demo until later.

@Mottie
Copy link
Owner Author

Mottie commented Jan 6, 2013

Ok, I made this demo. And this is the core javascript coding needed:

var minRows = 3, // Minimum number of rows needed before tablesorter allows sorting

  $t = $('table'),

  // check number of rows; enable or disable sorting
  checkRows = function () {
    // check number of rows
    var min = $t.find('tbody tr').length < minRows;
    // go through each header and enable/disable as needed
    $t[0].config.$headers.each(function (i) {
      // only enable/disable certain columns (ignore 5th column, it's always disabled)
      if (i < 4) {
        // disable sort per column
        this.sortDisabled = min;
        // add sorter-false class to hide controls
        $(this).toggleClass('sorter-false', min);
      }
    });
  };

$t
  // check number of rows after initialization & updates
  .on('tablesorter-initialized updateComplete', checkRows)
  // initialize tablesorter
  .tablesorter({
    theme: 'blue',
    widgets: ['zebra', 'columns']
  });

@Mottie
Copy link
Owner Author

Mottie commented Jan 7, 2013

Update: To remove default tablesorter styling, include this change in the code (demo):

if (i < 4) {
  // disable sort per column
  this.sortDisabled = min;
  // remove table styling completely when not enabled
  $t.toggleClass('tablesorter-blue', !min);
  $(this).toggleClass('tablesorter-header', !min);
  // add sorter-false class to hide controls
  $(this).toggleClass('sorter-false', min);
}

@Mottie
Copy link
Owner Author

Mottie commented Mar 1, 2013

I'm guessing this issue has been resolved, so I'm closing it. Please feel free to reopen it if you continue to have problems.

@Mottie Mottie closed this as completed Mar 1, 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

1 participant