Difference between #207 and #208 of
Yii v2 snippet guide

Revision #208 has been created by rackycz on Jul 3, 2020, 12:46:05 PM with the memo:

GridViev - Variable page size
« previous (#207) next (#209) »

Changes

Title unchanged

Yii v2 snippet guide

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2

Content changed

[...]
GridView cannot display DropDownList which could be used by the user to change the number of rows per page. You have to add it manually like this:

When you are creating a new model using Gii, you can select if you want to create the SearchModel as well. Do it, it is usefull for example in this situation. Then add following rows to the model:

```php
// file models/InvoiceSearch.php
 
 
use yii\helpers\Html; // add this row

class InvoiceSearch extends Invoice
[...]
// ...

//
aAdd followingthis functions:
public function getPageSizeDropDown($htmlOptions = [], $prefixHtml = '', $suffixHtml = '', $labelPrefix = '') {
return $prefixHtml . Html::activeDropDownList($this, 'pageSize',
[...]
}

    // Add this function:
 
public function getPageSizeDropDownID($prefix = '#') { return $prefix . Html::getInputId($this, 'pageSize'); }     // This function already exists, so change it:
 
public function search($params) { // .. $this->load($params);         
 
        // Add following rows:
if (!isset($this->pageSize)) { // Here we make sure that the dropDownLst will have correct value preselected $this->pageSize = $dataProvider->pagination->defaultPageSize; }         
 
$dataProvider->pagination->pageSize = (int)$this->pageSize;         
 
```

And then in your views/xxx/index.php use following:
[...]
'filterModel' => $searchModel,
'layout'=>'{summary}<br>{items}<br><div style="display:flex; background-color: #f9f9f9; padding: 0px 3rem;"><div style="flex-grow: 2;">{pager}</div><div style="align-self:center;">'.$pageSizeDropDown.'</div></div>',
'pager' => [ 'maxButtonCount' =>
320 ], 'filterSelector' => $searchModel->getPageSizeDropDownID(), // filterSelector is the core solution of this problem. It refreshes the grid. ```
7 0
4 followers
Viewed: 274 883 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Sep 19, 2019
Last updated: a year ago
Update Article

Revisions

View all history