YII2 framework LinkPager and Pagination Pagination

YII use paging, step by step teach you how to do

Pagination Pagination class LinkPager and can customize which attributes

First, we take a look at yii2 own paging class how to use?

1, controller action, for example the list of articles

use yii\data\Pagination;
$query = Article::find()->where(['status' => 1]);
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
$models = $query->offset($pages->offset)
    ->limit($pages->limit)
    ->all();
return $this->render('index', [
    'models' => $models,
    'pages' => $pages,
]);

2、View

use yii\widgets\LinkPager;
// cycle through data
foreach ($models as $model) {
    // ......
}
// display page numbers
echo LinkPager::widget([
    'pagination' => $pages,
])

I believe most people can understand.

The second step, which comes with the pagination class attributes can be defined

Let me talk about LinkPager components

pagination parameters required, this is an instance of the class we Pagination

The default page of the class looks like this

909d21bec4-yii2page1.png

 

Page up and down buttons and 10 buttons

First, we modify the buttons up and down pages into Chinese

<?= LinkPager::widget([ 
    'pagination' => $pages, 
    'NextPageLabel' => 'next', 
    'PrevPageLabel' => 'Back', 
]); ?>

If you do not want to show up and down the page, prevPageLabel and nextPageLabel can be set to false

<?= LinkPager::widget([ 
    'pagination' => $pages, 
    'nextPageLabel' => false, 
    'prevPageLabel' => false, 
]); ?>

Home is also not displayed by default last page, if you need to, it can be set up

<?= LinkPager::widget([ 
    'pagination' => $pages, 
    'FirstPageLabel' => 'Home', 
    'LastPageLabel' => 'Last', 
]); ?>

If your data is too small, not two, not displayed by default paging, if you need to, you can set up hideOnSinglePage = false

<?= LinkPager::widget([ 
    'pagination' => $pages, 
    'hideOnSinglePage' => false, 
]); ?>

The default page displayed is 10, you can set the number of pages you want maxButtonCount show

<?= LinkPager::widget([ 
    'pagination' => $pages, 
    'maxButtonCount' => 5, 
]); ?>

Some people do not like the default style, you want to bring your own page styles, you can set options, do not forget to implement pre, next, disabled and other self-style

<?= LinkPager::widget([ 
    'pagination' => $pages, 
    'options' => ['class' => 'm-pagination'], 
]); ?>

Next we talk Pagination components

The default paging routing is like following

/controller/action?page=2&per-page=20

First of all, we have to specify the total number of totalCount, no this parameter, the page is no way to achieve

$pages = new Pagination([
    'totalCount' => $totalCount,
]);

The default number of pages is 20, you can set pageSize as you want

$pages = new Pagination([
    'totalCount' => $totalCount,
    'pageSizeParam' => false,
]);

We can also see the default page depends on the parameters page, if you want to change the parameter p, like setting pageParam = p

$pages = new Pagination([
    'totalCount' => $totalCount,
    'pageParam' => 'p',
]);

If your page is present in the home, you definitely want to believe /? P = 1 instead of / site / index? P = 1, we look at how hidden away route

$pages = new Pagination([
    'totalCount' => $totalCount,
    'route' => false,
]);

You may find pagination Pagination class has a bug, if only one of our data, but the page manually change the address bar = 20 time, it will display the data page = 1? Of course, this is very annoying most of the interface API. However, this is not a bug, but a friendly verification. ValidatePage = false set out to avoid the problem

$pages = new Pagination([
    'totalCount' => $totalCount,
    'validatePage' => false,
]);

 

Guess you like

Origin blog.csdn.net/tjls2008/article/details/91411454