Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP items per page helper

How generate from pagination helper a drop-down list with items per page? Default value is 20 items per page, I want to get something like this:

Show 
<select>
    <option value="10">10</option>
    <option value="25">25</option>
    <option value="50">50</option>
    <option value="100">100</option>
</select> 
entries

http://jsfiddle.net/HkWuH/

like image 823
jtomaszk Avatar asked Dec 11 '22 13:12

jtomaszk


2 Answers

As @BigToach mentioned I have add this to method in my controller

$this->paginate = array(
    'paramType' => 'querystring',
    'limit' => 30,
    'maxLimit' => 100
);

and add following code to my View

$limit = $this->params->query['limit'];
$options = array( 5 => '5', 10 => '10', 20 => '20' );

echo $this->Form->create(array('type'=>'get'));

echo $this->Form->select('limit', $options, array(
    'value'=>$limit, 
    'default'=>20, 
    'empty' => FALSE, 
    'onChange'=>'this.form.submit();', 
    'name'=>'limit'
    )
);
echo $this->Form->end();
like image 193
jtomaszk Avatar answered Jan 19 '23 02:01

jtomaszk


The results per page is driven by the 'limit' parameter in the paginate array

<?php
$this->paginate = array(
    'paramType' => 'querystring',
    'limit' => 30,
);
?>

You can override this by setting limit as a query string value. Ex:

?limit=40

From there you just need to build a drop down that changes the URL when they select the number per page.

You might also want to implement maxLimit so users don't change this value to an extremely large number.

like image 25
BigToach Avatar answered Jan 19 '23 01:01

BigToach