1

I'd like to be able to display all records without the actual need of pagination but still want to keep the functionality of having sortable column headers by using something like $this->Paginator->sort().

Currently I'm setting 'limit' => 100 because I know I won't ever have about more than about 50 records at a time and this works just fine, however I wanted to know if there was basically a way to accomplish the sortable columns without pagination some other way such as 'limit' => unlimited or another Component of CakePHP that would accomplish this.

Here's the Controller:

class GroupsController extends AppController {

    public function view($slug = null) {

        $group = $this->Group->findBySlug($slug);

        $this->paginate = array(
            'Person' => array(
                'conditions' => array('Person.group_id' => $group["Group"]['id']),
                'limit' => 100,
                'recursive' => 0
            )
        );

        $people = $this->paginate('Person');

        $this->set('people', $people);

    }
}        

Here's the view:

<table>
    <tr>
        <th><?php echo $this->Paginator->sort('Person.id','Id'); ?></th>         
        <th><?php echo $this->Paginator->sort('first_name', 'First Name'); ?></th>         
        <th><?php echo $this->Paginator->sort('last_name', 'Last Name'); ?></th>         
    </tr>
    <!-- Here is where we loop through the people -->
    <?php foreach ($people as $person): ?>
    <tr>
        <td><?php echo $person['Person']['id']; ?></td>
        <td><?php echo $person['Person']['first_name']; ?></td>
        <td><?php echo $person['Person']['last_name']; ?></td>
    </tr>
    <?php endforeach; ?>
    <?php unset($player); ?>
</table>
Marc
  • 16,170
  • 20
  • 76
  • 119
bigmike7801
  • 3,908
  • 9
  • 49
  • 77
  • I agree with @RichardAtHome - there's no reason not to use Paginator - if you don't want pages, just set the limit to an unreachable number. – Dave Nov 08 '12 at 18:33

1 Answers1

5

It's the pagination component that handles the column sorting. If you don't want to use the paginator (why not?) you'll have to write your own code to do it.

One minor tweak you could do to your code is:

'limit' => $this->Group->find('count')

Which will ensure you always have enough of limit to display the full table.

RichardAtHome
  • 4,293
  • 4
  • 20
  • 29
  • The reason I don't want to use the pagination is because I'm wanting to display the full team roster at one time. I have no desire to have pagination. I do like the suggestion of `'limit' => $this->Group->find('count')`, however it adds an additional db query. – bigmike7801 Nov 08 '12 at 17:12
  • @bigmike7801 what `$this->Paginator->sort()` is to get the records again, but sorted by the column it was used for and in the following order first click: ASC, next click DESC, nextClick ASC... There is no reason of why would you not want to use only the sorting capabilities. Also that extra COUNT query on a table that never has more than 50 records could not be an overhead-you will at least have a primary index - then the `COUNT(*)` should go for 0.0001 secs. [Here's](http://stackoverflow.com/questions/1659977/mysql-count-vs-mysql-select-which-one-is-faster) a bit more clarification on this. – Borislav Sabev Nov 09 '12 at 07:30