4

I render a table with theme_table() and use tablesorting (sql) for that. It all works fine.

Except for the intial/default sort on the other tables. No user sort option defined yet. Example:

Name | Age ▼

Sort by "age" is defined in the $header. User clicks on "Name". That now gets sorted ascending. Regardless! I need descending.

Click:

Name ▲ | Age

But I need/want:

Click:

Name ▼ | Age

Studying the code that determines the default sort order, in tablesort_header, I come across the following:

else {
  // If the user clicks a different header, we want to sort ascending initially.
  $ts['sort'] = 'asc';
  $image = '';
}

I see no way to avoid this. Do you? Did I miss some parameter, option? I refuse to believe that Drupal makes such a silly assumption without letting developers override it.

4 Answers 4

3

No, it isn't possible at this point.

There is an issue for that, I suggest you try that out and report back so that it can be commited soon: http://drupal.org/node/109493

2
  • If @berkes is using drupal-6 he will have a hard using this though, without patching core himself.
    – googletorp
    Commented Apr 28, 2011 at 13:15
  • This is for d6 indeed. But A core hack is not a great problem here :)
    – berkes
    Commented Apr 28, 2011 at 13:33
2

You can override it, but probably not the way you would like. tablesort_header is called from within theme_table. I guess the reason why it have been hardcoded like this, is that is's a utility function for a theme function.

You can override theme_table and change the behavior, though I must admit it would have been nicer to have it been determined by a variable.

1
  • I saw two solutions, both about as ugly: Create my own custom tablesort_header which takes some $header parameter into consideration. Or use a regular expression search-replace to grep out the "asc" from the link and turn that into "desc".
    – berkes
    Commented Apr 28, 2011 at 13:35
0

There is a work around, if you are able to define the link that takes you to the page with the table you want sorted.

Simply craft the link to be the same as if you had clicked on the column to make it descending. The link will have the table column name and sorting defined as fragments: www.domain.com/table_page?order=title&sort=desc

At least this is not a core hack, and it will persist over pagination links.

1
  • I think you misunderstood the original question. I am aware of these fractioned links. But in your case, title will be ordered default "desc". But the other sorting-headers are still ordered asc when clicking on them.
    – berkes
    Commented Jul 5, 2011 at 12:29
0

Here's how I did it with a custom module using #attached and a .js behavior:

example.js

(function ($) {
  Drupal.behaviors.myFoo = {
    attach: function (context, settings) {

      var th = document.querySelectorAll('#my-container table.sticky-table thead th');
      if (th) {
        for (var i = 0; i < th.length; i++) {
          var el = th[i];
          if (el.classList.contains('active')) { continue; } // Skip the active column.
          var a = el.querySelector('a');
          a.setAttribute('href', a.getAttribute('href').replace('sort=asc', 'sort=desc'));
        }
      }

    }
  };
}(jQuery));

It simply iterates over each column in the table (skipping the active column), the replaces the sort=asc with sort=desc on each link.

Quick and dirty, that's what she said.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.