Skip to content

Commit

Permalink
Dropdown select limit (view limit) option for Paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
eather009 committed Feb 24, 2017
1 parent 4180750 commit 78e7b84
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/View/Helper/PaginatorHelper.php
Expand Up @@ -28,6 +28,7 @@
* @property \Cake\View\Helper\UrlHelper $Url
* @property \Cake\View\Helper\NumberHelper $Number
* @property \Cake\View\Helper\HtmlHelper $Html
* @property \Cake\View\Helper\FormHelper $Form
* @link http://book.cakephp.org/3.0/en/views/helpers/paginator.html
*/
class PaginatorHelper extends Helper
Expand All @@ -40,7 +41,7 @@ class PaginatorHelper extends Helper
*
* @var array
*/
public $helpers = ['Url', 'Number', 'Html'];
public $helpers = ['Url', 'Number', 'Html', 'Form'];

/**
* Default config for this class
Expand Down Expand Up @@ -1169,4 +1170,37 @@ public function implementedEvents()
{
return [];
}

/**
* dropdown array for Select limit
*
* @param array $limits This is array of options.
* @param int|null $default Default limit for option selecting. Default value is $this->param('perPage').
* @param array $options Options for Select tag attributes like class, id or event
*
* @return string html output.
*/
public function limitControl(array $limits = [], $default = null, array $options = [])
{
$out = $this->Form->create(null, ['type' => 'get']);

if (empty($default) || !is_numeric($default)) {
$default = $this->param('perPage');
}

if (empty($limits)) {
$limits += [20 => '20', 25 => '25', 50 => '50', 100 => '100'];
}

if (!in_array($default, $limits)) {
$limits += [$default => $default];
}

ksort($limits);

$out .= $this->Form->control('limit', ($options + ['type' => 'select', 'label' => __("View"), 'value' => $default, 'options' => $limits, 'onChange' => 'this.form.submit()']));
$out .= $this->Form->end();

return $out;
}
}
49 changes: 49 additions & 0 deletions tests/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -2546,4 +2546,53 @@ public function testMeta($page, $prevPage, $nextPage, $pageCount, $options, $exp

$this->assertSame($expected, $this->View->fetch('meta'));
}

/**
* test the limitControl() method
*
* @return void
*/
public function testLimitControl()
{
$out = $this->Paginator->limitControl([1 => 1]);
$expected = [
['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
['div' => ['class' => 'input select']],
['label' => ['for' => 'limit']],
'View',
'/label',
['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()']],
['option' => ['value' => '']],
'/option',
['option' => ['value' => '1']],
'1',
'/option',
'/select',
'/div',
'/form'
];
$this->assertHtml($expected, $out);

$out = $this->Paginator->limitControl([1 => 1, 5 => 5], null, ['class' => 'form-control']);
$expected = [
['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
['div' => ['class' => 'input select']],
['label' => ['for' => 'limit']],
'View',
'/label',
['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()', 'class' => 'form-control']],
['option' => ['value' => '']],
'/option',
['option' => ['value' => '1']],
'1',
'/option',
['option' => ['value' => '5']],
'5',
'/option',
'/select',
'/div',
'/form'
];
$this->assertHtml($expected, $out);
}
}

0 comments on commit 78e7b84

Please sign in to comment.