Page Size - Paginating GridView Yii2
Create a file PageSize.php , copy code below into it.
class PageSize extends \yii\base\Widget {
/**
* @var string the label text.
*/
public $label = 'Show ';
/**
* @var integer the defualt page size. This page size will be used when the $_GET['pageSize'] is empty.
*/
const DefaultPageSize = 10;
/**
* @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 = 'pageSize';
/**
* @var array the list of page sizes
*/
public $sizes = [5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 50 => 50, 100 => 100];
/**
* @var string the template to be used for rendering the output.
*/
public $template = '{label} {list} entries';
/**
* @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] : self::DefaultPageSize;
$listHtml = Html::dropDownList('pageSize', $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;
}
}
In GridView you can call below code:
/* ~~~ * * and set the `filterSelector` property of GridView as shown in * following example. * * ~~~ * $dataProvider, * 'filterModel' => $searchModel, * 'filterSelector' => 'select[name="pageSize"]', //Copy this code * 'columns' => [ * ... * ], * ]); ?> * ~~~ */
Comments
Post a Comment