nterms / yii2-pagesize-widget

Widget for enabling dynamic page size selection on yii2 GridView
MIT License
38 stars 18 forks source link

Doesn't work for more than 50 items #7

Open sinaza opened 8 years ago

sinaza commented 8 years ago

Thanks for the great work.

For me, displaying more than 50 items per page is not working and it automatically limits itself to 50.

Because of this, and the fact that an "All" option is needed, I've added if(isset($_GET['per-page'])) $dataProvider->pagination->pageSize = $_GET['per-page'];

inside the actionIndex() which fixes both problems. Oh and I put 'all'=>'All' in the sizes array too.

nterms commented 8 years ago

Thanks for the info, I'll look into this issue soon. By the way, glad that you found a way around this.

alvinux commented 8 years ago

add this before class PageSize

\Yii::$container->set('yii\data\Pagination', [
   'pageSizeLimit' => [1, 200],
]);
marekpetras commented 7 years ago

Hey,

this is not a bug, its intended pagination limitation that you have to override in the DI container. I extended gridview to include everything by itself:

<?php

namespace app\widgets;

use Yii;
use nterms\pagesize\PageSize;

/**
 * adds a pagesize widget by default to the grid
 */
class GridView extends \yii\grid\GridView
{
    /**
     * @var default filter selector for Pagesize widget
     */
    public $filterSelector = 'select[name="per-page"]';

    /**
     * @var default filter selector for Pagesize widget
     */
    public $layout = "{pagesize}\n{summary}\n{items}\n{pager}";

    /**
     * @var pageSizeLimit for the pagination service
     */
    public $pageSizeLimit = [1,200];

    /**
     * @var defaultPageSize for the pagination service and the Pagesize widget
     */
    public $defaultPageSize = 50;

    /**
     * @inheritdoc
     */
    public function init()
    {
        Yii::$container->set('yii\data\Pagination', [
           'pageSizeLimit' => $this->pageSizeLimit,
           'defaultPageSize' => $this->defaultPageSize,
        ]);

        parent::init();
    }

    /**
     * @inheritdoc
     */
    public function renderSection($name)
    {
        switch ($name) {
            case '{pagesize}':
                return $this->renderPagesize();
            default:
                return parent::renderSection($name);
        }
    }

    /**
     * Renders the pagesize widget.
     * @return string the rendering result
     */
    public function renderPagesize()
    {
        return PageSize::widget(['defaultPageSize' => Yii::$container->get('yii\data\Pagination')->defaultPageSize]);
    }
}

works like a charm.

LeoZandvliet commented 6 years ago

This issue finally got me on track of the 'misbehaving' gridview! :D

@marekpetras I tried your nice solution but found out I had to add the pagination object to the dataProvider of the GridView to get the pageSizeLimit to work. Probaly because it already exists before GridView->init() is called. Is this correct? The following works for me:

public function init()
{
    Yii::$container->set('yii\data\Pagination', [
           'pageSizeLimit' => $this->pageSizeLimit,
           'defaultPageSize' => $this->defaultPageSize,
    ]);

    if($this->dataProvider)
    {
    $this->dataProvider->pagination = ['pageSizeLimit' => $this->pageSizeLimit, 'defaultPageSize' => $this->defaultPageSize];
    }

    parent::init();
}
marekpetras commented 6 years ago

@LeoZandvliet hm, i dont think i ve ever needed to do any changes to the pagination in my searchmodel or anything, basically, i dont think i ve actually ever touched pagination directly, you are better of modifying it via query parameters as intended. And if for some reason you modify the pagination before you create the gridview, this approach will drop the old pagination and create a new one