nuts and bolts of cakephp

Displaying sort direction in paginated data

Posted in CakePHP by teknoid on July 28, 2008

Just a little tip on styling your table with paginated data…

Wouldn’t it be nice to show to the user what column and direction is currently used for sorting of your data? Here’s how you can do that with a little CSS and some Paginator methods.

First, we need to find out in our view which column we use to sort and in what direction (ASC or DESC) the sorting goes:

$sortDir = $paginator->sortDir();
$sortKey = $paginator->sortKey();

Now that we know the two values, we can easily style the table header:

<th <?php echo ($sortKey == 'ID') ? 'class="'.$sortDir.'"' : ''; ?>>

In the example above, if our sorting field happens to be “ID” we assign a class=”asc” (for ascending) or class=”desc” (for descending), else no class is assigned.
This code uses a short-hand if/else condition, which is sometimes frowned upon, but I feel in this case it provides for a cleaner looking code.

2 Responses

Subscribe to comments with RSS.

  1. noname- said, on July 29, 2008 at 2:55 am

    Exactly what I was looking for, thanks!

  2. teknoid said, on July 29, 2008 at 10:19 am

    @noname-

    You’re welcome


Leave a Reply