PHPackages                             antonyz89/yii2-pagesize - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. antonyz89/yii2-pagesize

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

antonyz89/yii2-pagesize
=======================

dynamic page size for gridview

0.0.3(4y ago)01.2k2BSD-3-ClausePHPPHP &gt;=7

Since Oct 12Pushed 4y ago1 watchersCompare

[ Source](https://github.com/AntonyZ89/yii2-pagesize)[ Packagist](https://packagist.org/packages/antonyz89/yii2-pagesize)[ RSS](/packages/antonyz89-yii2-pagesize/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (2)

yii2-pagesize
=============

[](#yii2-pagesize)

[ ![Donate with PayPal](https://camo.githubusercontent.com/648ad6f048733f167bf65e11a4fd759eef14da88db61ad078bbd5ddea5d57133/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f4c472e676966)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YATHVT293SXDL&source=url)\--

[![Latest Stable Version](https://camo.githubusercontent.com/563fde31af9938ebf1555058f5bd11491f03eb53ec4cfbfea4fa8dfe7a68a493/68747470733a2f2f706f7365722e707567782e6f72672f616e746f6e797a38392f796969322d7061676573697a652f762f737461626c65)](https://packagist.org/packages/antonyz89/yii2-pagesize)[![Total Downloads](https://camo.githubusercontent.com/2e874573e6c694fd963f0bce37622cc975204af9f96af85a2fd4e2828d875e36/68747470733a2f2f706f7365722e707567782e6f72672f616e746f6e797a38392f796969322d7061676573697a652f646f776e6c6f616473)](https://packagist.org/packages/antonyz89/yii2-pagesize)[![Latest Unstable Version](https://camo.githubusercontent.com/275c89321fbe84e5e916cc9b1f48135fd83301eb7627cf10636f5a64569b25ec/68747470733a2f2f706f7365722e707567782e6f72672f616e746f6e797a38392f796969322d7061676573697a652f762f756e737461626c65)](https://packagist.org/packages/antonyz89/yii2-pagesize)[![License](https://camo.githubusercontent.com/8e37737a7701a84a65090916dd22314f268c2b98b29c4e453bf1c0ce361d4a58/68747470733a2f2f706f7365722e707567782e6f72672f616e746f6e797a38392f796969322d7061676573697a652f6c6963656e7365)](https://packagist.org/packages/antonyz89/yii2-pagesize)

- [Installation](#installation)
- [Usage](#usage)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist antonyz89/yii2-pagesize dev-master

```

or add

```
"antonyz89/yii2-pagesize": "dev-master"

```

to the require section of your `composer.json` file.

USAGE
-----

[](#usage)

1 - Add the translation:

`common/config/main.php`

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

2 - Add `panel -> footer` to your GridView:

```
use antonyz89\pagesize\PageSize;

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

GridView::widget([
    ...
    'panelFooterTemplate' => '{footer}',
    'filterSelector' => '#per-page', // with #
    'panel' => [
        'footer' => "
            {pager} {summary}

                $pageSize

        "
    ],
    ...
]);
```

Optional
--------

[](#optional)

1 - In your `common/config/bootstrap.php`, you can override default values:

```
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
    ]);
};
```

2 - Create a GridView for you! Avoid duplicate code.

**Tip:** Create your new component in `common/components`

```
