antonyz89/yii2-pagesize

动态分页大小用于GridView

安装: 783

依赖者: 2

建议者: 0

安全性: 0

星标: 0

关注者: 3

分支: 0

类型:yii2-extension

0.0.3 2021-08-12 19:55 UTC

This package is auto-updated.

Last update: 2024-09-13 02:23:37 UTC


README

Donate with PayPal

--

Latest Stable Version Total Downloads Latest Unstable Version License

安装

安装此扩展的首选方式是通过 Composer

运行以下命令之一

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

或者

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

将以下内容添加到您的 composer.json 文件的 require 部分。

用法

1 - 添加翻译

common/config/main.php

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

2 - 将 panel -> footer 添加到您的 GridView

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>
        "
    ],
    ...
]);

可选

1 - 在您的 common/config/bootstrap.php 中,您可以覆盖默认值

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 - 为您创建一个 GridView!避免代码重复。

提示:common/components 中创建您的新组件

<?php

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();
    }
}

使用自定义分页大小 ID

1 - 更新 SearchModel 上的 ActiveDataProvider

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

2 - 将 panel -> footer 添加到您的 GridView

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>
        "
    ],
    ...
]);

2.1 - 如果您创建了自定义的 GridView,您只需覆盖 filterSelector

use my\own\GridView;


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