-
Notifications
You must be signed in to change notification settings - Fork 18
/
PageSize.php
106 lines (90 loc) · 2.67 KB
/
PageSize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* @copyright Copyright © Saranga Abeykoon, nterms.com, 2014
* @package yii2-pagesize-widget
* @version 1.0.0
*/
namespace nterms\pagesize;
use yii\helpers\Html;
/**
* PageSize widget is an addition to the \yii\grid\GridView that enables
* changing the size of a page on GridView.
*
* To use this widget with a GridView, add this widget to the page:
*
* ~~~
* <?php echo \nterms\PageSize::widget(); ?>
* ~~~
*
* and set the `filterSelector` property of GridView as shown in
* following example.
*
* ~~~
* <?= GridView::widget([
* 'dataProvider' => $dataProvider,
* 'filterModel' => $searchModel,
* 'filterSelector' => 'select[name="per-page"]',
* 'columns' => [
* ...
* ],
* ]); ?>
* ~~~
*
* Please note that `per-page` here is the string you use for `pageSizeParam` setting of the PageSize widget.
*
* @author Saranga Abeykoon <[email protected]>
* @since 1.0
*/
class PageSize extends \yii\base\Widget
{
/**
* @var string the label text.
*/
public $label = 'items';
/**
* @var integer the defualt page size. This page size will be used when the $_GET['per-page'] is empty.
*/
public $defaultPageSize = 20;
/**
* @var string the name of the GET request parameter used to specify the size of the page.
* This will be used as the input name of the dropdown list with page size options.
*/
public $pageSizeParam = 'per-page';
/**
* @var array the list of page sizes
*/
public $sizes = [2 => 2, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 50 => 50, 100 => 100, 200 => 200];
/**
* @var string the template to be used for rendering the output.
*/
public $template = '{list} {label}';
/**
* @var array the list of options for the drop down list.
*/
public $options;
/**
* @var array the list of options for the label
*/
public $labelOptions;
/**
* @var boolean whether to encode the label text.
*/
public $encodeLabel = true;
/**
* Runs the widget and render the output
*/
public function run()
{
if(empty($this->options['id'])) {
$this->options['id'] = $this->id;
}
if($this->encodeLabel) {
$this->label = Html::encode($this->label);
}
$perPage = !empty($_GET[$this->pageSizeParam]) ? $_GET[$this->pageSizeParam] : $this->defaultPageSize;
$listHtml = Html::dropDownList($this->pageSizeParam, $perPage, $this->sizes, $this->options);
$labelHtml = Html::label($this->label, $this->options['id'], $this->labelOptions);
$output = str_replace(['{list}', '{label}'], [$listHtml, $labelHtml], $this->template);
return $output;
}
}