PHP code example of antonyz89 / yii2-pagesize

1. Go to this page and download the library: Download antonyz89/yii2-pagesize library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

antonyz89 / yii2-pagesize example snippets


return [
    ...
    'components' => [
        'i18n' => [
            'translations' => [
                'pagesize' => [
                    'class' => \yii\i18n\PhpMessageSource::class,
                    'basePath' => '@antonyz89/pagesize/messages',
                ]
            ]
        ]
    ],
    ...
];

use antonyz89\pagesize\PageSize;

$pageSize = PageSize::widget([
    'options' => [
        'id' => 'per-page' // without #
    ]
]);

GridView::widget([
    ...
    'panelFooterTemplate' => '{footer}<div class="clearfix"></div>',
    'filterSelector' => '#per-page', // with #
    'panel' => [
        'footer' => "
            {pager} {summary}
            <div class='float-right'>
                $pageSize
            </div>
        "
    ],
    ...
]);

use antonyz89\pagesize\PageSize;

PageSize::$defaultPageSize = 10;
PageSize::$values = [10, 20, 30, 40, 50];

/* `PageSize::$renderItem` to being used in `$renderSelect` */
PageSize::$renderItem = static function ($value, $key, $page) {
    return [$key, $value];
};

/*
 * `PageSize::$renderSelect`, use for render a custom select.
 * If needed override $renderItem to return `$items` as you want
 */
PageSize::$renderSelect = static function (array $options, array $items, string $pageSize) {
    $items = array_combine(
        array_map(static function ($value) {
            return $value[0];
        }, $items),
        array_map(static function ($value) {
            return $value[1];
        }, $items)
    );


    return Select2::widget([
        'name' => $options['name'],
        'id' => $options['id'],
        'data' => $items,
        'value' => $pageSize,
        'hideSearch' => true,
        'theme' => Select2::THEME_MATERIAL
    ]);
};



namespace common\components;

use antonyz89\pagesize\PageSize;

class GridView extends \yii\grid\GridView
{
    public $panelFooterTemplate = '{footer}<div class="clearfix"></div>';
    public $filterSelector = '#pagesize';

    protected function initPanel()
    {
        $pageSize = PageSize::widget([
            'options' => [
                'id' => str_replace('#', '', $this->filterSelector) // without #
            ]
        ]);
    
        $this->panel['footer'] = "
            {pager} {summary}
            <div class='float-right'>
                $pageSize
            </div>
        ";

        parent::initPanel();
    }
}

use antonyz89\pagesize\PageSizeTrait;

public class ExampleSearch extends Example {
    use PageSizeTrait; // add PageSizeTrait
    
    public $pageSizeId = 'custom-pagesize'; // custom ID

    public function search($params)
    {
        ...
        $dataProvider = new ActiveDataProvider([
            ...
            'pagination' => $this->pagination, // add `$this->pagination`
        ]);
        ...
    }
}

use antonyz89\pagesize\PageSize;

$pageSize = PageSize::widget([
    'options' => [
        'id' => 'custom-pagesize' // without #
    ]
]);

GridView::widget([
    ...
    'panelFooterTemplate' => '{footer}<div class="clearfix"></div>',
    'filterSelector' => '#custom-pagesize', // with #
    'panel' => [
        'footer' => "
            {pager} {summary}
            <div class='float-right'>
                $pageSize
            </div>
        "
    ],
    ...
]);

use my\own\GridView;


GridView::widget([
    ...
    'filterSelector' => '#custom-pagesize', // with #
    ...
]);