Thursday, July 23, 2009

How to make tablesorting work with multiple tables on the same page

With Drupal 5.0 and beyond, it is now possible to provide independent sorting of tables when they appear on the same page.

This is accomplished by inserting a unique label for each table in both the tablesort_sql and theme('table'... calls for each relevant table.

For example, suppose you had two sortable tables on a page, one a list of users, and the other a list of nodes. To make each table sortable independently, you would do something like:

For the user table:
...
$user_sort = tablesort_sql($header, NULL, 'user');

...and then in the user table theme call...
$output = theme('table', $header, $rows, array('class' => 'some-class'), NULL, 'user');

For the node table:
...
$node_sort = tablesort_sql($header, NULL, 'node');

...and then in the node table theme call...
$output = theme('table', $header, $rows, array('class' => 'some-class'), NULL, 'node');

Note that for each table, the label you use for tablesort_sql and theme('table'... must be identical for the independent sorting to be handled properly.

No comments: