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

Revision #207 has been created by rackycz on Jul 3, 2020, 12:42:50 PM with the memo:

GridViev - Variable page size
« previous (#206) next (#208) »

Changes

Title unchanged

Yii v2 snippet guide

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2

Content changed

[...]
$currencies = ArrayHelper::map($currencies, 'id', 'name');

<?= $form->field($model, 'id_currency')->dropDownList($currencies) ?>
```

Note: In other views you will need models with predefined relations to reach the correct value. Relations can be created using GII (when they are defined in DB) or [manually](https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#relational-data).

 
 
**GridViev - Variable page size**
 
---
 
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
 
use yii\helpers\Html; // add this row
 
 
class InvoiceSearch extends Invoice
 
{
 
  public $pageSize = null // add this row
 
  // ...
 
  
 
  public function rules()
 
  {
 
    return [ // ...
 
      ['pageSize', 'safe'], // add this row
 
      // ...
 
  
 
  // add following functions:
 
  public function getPageSizeDropDown($htmlOptions = [], $prefixHtml = '', $suffixHtml = '', $labelPrefix = '') {
 
    return $prefixHtml . Html::activeDropDownList($this, 'pageSize',
 
      [
 
        10 => $labelPrefix.'10', 
 
        20 => $labelPrefix.'20', 
 
        50 => $labelPrefix.'50', 
 
        100 => $labelPrefix.'100', 
 
        150 => $labelPrefix.'150', 
 
        200 => $labelPrefix.'200', 
 
        300 => $labelPrefix.'300', 
 
        500 => $labelPrefix.'500', 
 
        1000 => $labelPrefix.'1000'
 
      ],$htmlOptions ) . $suffixHtml;
 
    }
 
 
    public function getPageSizeDropDownID($prefix = '#') {
 
      return $prefix . Html::getInputId($this, 'pageSize');
 
    }
 
    
 
    public function search($params)
 
    {
 
        // ..
 
        $this->load($params);
 
 
        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:
 
 
```php
 
$pageSizeDropDown = $searchModel->getPageSizeDropDown(['class' => 'form-control', 'style'=>'width: 20rem'],'','','Rows per page: ');
 
 
echo GridView::widget([
 
  'dataProvider' => $dataProvider,
 
  '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' => 30 ],
 
  
 
  'filterSelector' => $searchModel->getPageSizeDropDownID(),
 
  // filterSelector the core solution of this problem. It refreshes the grid.
 
```
 
 
 
7 0
4 followers
Viewed: 259 235 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Sep 19, 2019
Last updated: 7 months ago
Update Article

Revisions

View all history