jtomaszk
jtomaszk

Reputation: 11191

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/

Upvotes: 4

Views: 3635

Answers (4)

user1077915
user1077915

Reputation: 894

I think you could use :

// Use the defaults.

echo $this->Paginator->limitControl();

More information at

https://book.cakephp.org/3.0/en/views/helpers/paginator.html#creating-a-limit-selectbox-control

Upvotes: 2

mvemjsunp
mvemjsunp

Reputation: 331

(This is really just a comment on Warthel4578's answer, but I don't have enough rep to comment yet... just wanted to share this tip for anyone who runs into the same issue I had with this solution)

So I kept getting the error "Undefined index: limit" (on the line that sets $limit = $this->params->query['limit'];) upon initial page load of my paginated results, although after changing the dropdown value and the paginated results refreshing, the error went away.

In place of $limit = $this->params->query['limit'];, I had to use this in my View:

$page_params = $this->Paginator->params();
$limit = $page_params['limit'];

Upvotes: 2

jtomaszk
jtomaszk

Reputation: 11191

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();

Upvotes: 7

BigToach
BigToach

Reputation: 580

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.

Upvotes: 2

Related Questions