├── LICENSE.md ├── PageSize.php ├── README.md └── composer.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Saranga Abeykoon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /PageSize.php: -------------------------------------------------------------------------------- 1 | 20 | * ~~~ 21 | * 22 | * and set the `filterSelector` property of GridView as shown in 23 | * following example. 24 | * 25 | * ~~~ 26 | * $dataProvider, 28 | * 'filterModel' => $searchModel, 29 | * 'filterSelector' => 'select[name="per-page"]', 30 | * 'columns' => [ 31 | * ... 32 | * ], 33 | * ]); ?> 34 | * ~~~ 35 | * 36 | * Please note that `per-page` here is the string you use for `pageSizeParam` setting of the PageSize widget. 37 | * 38 | * @author Saranga Abeykoon 39 | * @since 1.0 40 | */ 41 | class PageSize extends \yii\base\Widget 42 | { 43 | /** 44 | * @var string the label text. 45 | */ 46 | public $label = 'items'; 47 | 48 | /** 49 | * @var integer the defualt page size. This page size will be used when the $_GET['per-page'] is empty. 50 | */ 51 | public $defaultPageSize = 20; 52 | 53 | /** 54 | * @var string the name of the GET request parameter used to specify the size of the page. 55 | * This will be used as the input name of the dropdown list with page size options. 56 | */ 57 | public $pageSizeParam = 'per-page'; 58 | 59 | /** 60 | * @var array the list of page sizes 61 | */ 62 | public $sizes = [2 => 2, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 50 => 50, 100 => 100, 200 => 200]; 63 | 64 | /** 65 | * @var string the template to be used for rendering the output. 66 | */ 67 | public $template = '{list} {label}'; 68 | 69 | /** 70 | * @var array the list of options for the drop down list. 71 | */ 72 | public $options; 73 | 74 | /** 75 | * @var array the list of options for the label 76 | */ 77 | public $labelOptions; 78 | 79 | /** 80 | * @var boolean whether to encode the label text. 81 | */ 82 | public $encodeLabel = true; 83 | 84 | /** 85 | * Runs the widget and render the output 86 | */ 87 | public function run() 88 | { 89 | if(empty($this->options['id'])) { 90 | $this->options['id'] = $this->id; 91 | } 92 | 93 | if($this->encodeLabel) { 94 | $this->label = Html::encode($this->label); 95 | } 96 | 97 | $perPage = !empty($_GET[$this->pageSizeParam]) ? $_GET[$this->pageSizeParam] : $this->defaultPageSize; 98 | 99 | $listHtml = Html::dropDownList($this->pageSizeParam, $perPage, $this->sizes, $this->options); 100 | $labelHtml = Html::label($this->label, $this->options['id'], $this->labelOptions); 101 | 102 | $output = str_replace(['{list}', '{label}'], [$listHtml, $labelHtml], $this->template); 103 | 104 | return $output; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | yii2-pagesize-widget 2 | ==================== 3 | 4 | PageSize widget is an extension to the [Yii2](https://github.com/yiisoft/yii2) [GridView](http://www.yiiframework.com/doc-2.0/yii-grid-gridview.html) that enables 5 | changing the size of a page on GridView. 6 | 7 | [![Latest Stable Version](https://poser.pugx.org/nterms/yii2-pagesize-widget/v/stable)](https://packagist.org/packages/nterms/yii2-pagesize-widget) [![Total Downloads](https://poser.pugx.org/nterms/yii2-pagesize-widget/downloads)](https://packagist.org/packages/nterms/yii2-pagesize-widget) [![Latest Unstable Version](https://poser.pugx.org/nterms/yii2-pagesize-widget/v/unstable)](https://packagist.org/packages/nterms/yii2-pagesize-widget) [![License](https://poser.pugx.org/nterms/yii2-pagesize-widget/license)](https://packagist.org/packages/nterms/yii2-pagesize-widget) 8 | 9 | Installation 10 | ------------ 11 | 12 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 13 | 14 | Either run 15 | 16 | ``` 17 | php composer.phar require --prefer-dist nterms/yii2-pagesize-widget "*" 18 | ``` 19 | 20 | or add 21 | 22 | ``` 23 | "nterms/yii2-pagesize-widget": "*" 24 | ``` 25 | 26 | to the require section of your `composer.json` file. 27 | 28 | 29 | Usage 30 | ----- 31 | 32 | To use this widget with a GridView, add this widget to the view where the GridView is: 33 | 34 | ~~~php 35 | 36 | ~~~ 37 | 38 | and set the `filterSelector` property of GridView as shown in 39 | following example. 40 | 41 | ~~~php 42 | $dataProvider, 44 | 'filterModel' => $searchModel, 45 | 'filterSelector' => 'select[name="per-page"]', 46 | 'columns' => [ 47 | ... 48 | ], 49 | ]); ?> 50 | ~~~ 51 | 52 | Please note that `per-page` here is the string you use for `pageSizeParam` setting of the PageSize widget. 53 | 54 | Configurations 55 | -------------- 56 | 57 | Following properties are available for customizing the widget. 58 | 59 | - `label`: Text for the lbel 60 | - `defaultPageSize`: This value will be used if there's no page size selected 61 | - `pageSizeParam`: The name of the page size parameter used for the pagination widget in your grid view 62 | - `sizes`: An array of key values to be used as page sizes. Both kay and value should be integers 63 | - `template`: A template string to be used for rendering the elements. Default is `'{list} {label}'` 64 | - `options`: HTML attributes for the `