Yii2扩展 - LinkPager 带分页大小

安装:

1. 安装 Composer-Setup.exe

2. 在程序根目录右键(同时按住"shift")打开 "Use Composer here", 执行命令(会同时更新yii2的核心库):

composer require --prefer-dist liyunfang/yii2-widget-linkpager

使用:

1. 在输出 GridView 时配置如下属性:

GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ……
    ],
    'filterSelector' => "select[name='" . $dataProvider->getPagination()->pageSizeParam . "'], input[name='" . $dataProvider->getPagination()->pageParam . "']",
    'pager' => [
        'class' => \liyunfang\pager\LinkPager::className(),
        'template' => '{pageButtons} {customPage} {pageSize}',
        'pageSizeList' => [10, 20, 30, 50],
        'customPageWidth' => 50,
        'customPageBefore' => ' 跳转到第 ', 
        'customPageAfter' => ' 页 ',
    ],
]);

2. 在对应的控制器中添加:

$searchModel = new ArticleSearch();
$pageSize = isset($_GET['per-page']) ? intval($_GET['per-page']) : 2;
$dataProvider = new ActiveDataProvider([
    'query' => $searchModel->find()->orderBy('sort desc, publish_time desc'),
    'pagination' => [
        'pagesize' => $pageSize,
    ]
]);

3. "pager"的配置参数:

template             string    分页栏布局, 默认为"{pageButtons} {pageSize}"
    pageButtons   页码列表
    customPage    第几页
    pageSize      页大小下拉框
pageSizeList         array     页大小下拉框值, 默认为"[10, 20, 30, 50]"
pageSizeMargin       string    "pageSize"的 css 属性, 默认为"margin-left:5px;margin-right:5px;"
pageSizeOptions      string    "pageSize"的 html 属性, 默认为"['class'=>'form-control', 'style'=>'display:inline-block;width:auto;margin-top:0px;']"
customPageBefore     string    "customPage"之前的文本, 默认为空
customPageAfter      string    "customPage"之后的文本, 默认为空
customPageWidth      integer   "customPage"文本框的宽度, 单位为"px", 默认为"80"
customPageMargin     string    "customPage"的 css 属性, 默认为空
customPageOptions    string    "customPage"的 html 属性, 默认为"['class'=>'form-control', 'style'=>'display:inline-block;margin-top:0px;']"