服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - Yii2分页的使用及其扩展方法详解

Yii2分页的使用及其扩展方法详解

2021-01-22 16:02白狼 PHP教程

这篇文章主要介绍了Yii2分页的使用及其扩展方法详解的相关资料,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下

前言:

说明下我们本篇文章都要讲哪些内容

分页的使用,一步一步的教你怎么做

分页类linkpager和pagination都可以自定义哪些属性

分页类linkpager如何扩展成我们所需要的

第一步,我们来看看yii2自带的分页类该如何去使用?

1、controller action

?
1
2
3
4
5
6
7
8
9
10
11
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

?
1
2
3
4
5
6
7
8
9
use yii\widgets\linkpager;
//循环展示数据
foreach ($models as $model) {
  // ......
}
//显示分页页码
echo linkpager::widget([
  'pagination' => $pages,
])

代码基本上可以完全拷贝,修改部分数据即可,相信大多数人都是看得懂的。

我们接下来看第二步,自带的分页类都可以定义哪些属性

首先我们说说linkpager组件

.pagination参数必填,这个是我们pagination类的实例

默认分页类是下面这个样子的

 Yii2分页的使用及其扩展方法详解

.上下页按钮以及10个按钮

首先,我们把上下页的按钮修改成中文

?
1
2
3
4
5
<?= linkpager::widget([
  'pagination' => $pages,
  'nextpagelabel' => '下一页',
  'prevpagelabel' => '上一页',
]); ?>

如果你不想要显示上下页,可以将prevpagelabel和nextpagelabel设置为false

?
1
2
3
4
5
<?= linkpager::widget([
  'pagination' => $pages,
  'nextpagelabel' => false,
  'prevpagelabel' => false,
]); ?>

默认不显示首页也尾页,如果你需要,可以这样设置

?
1
2
3
4
5
<?= linkpager::widget([
  'pagination' => $pages,
  'firstpagelabel' => '首页',
  'lastpagelabel' => '尾页',
]); ?>

如果你的数据过少,不够2页,默认不显示分页,如果你需要,设置hideonsinglepage=false即可

?
1
2
3
4
<?= linkpager::widget([
  'pagination' => $pages,
  'hideonsinglepage' => false,
]); ?>

默认显示的页码为10页,可以设置maxbuttoncount为你想要展示的页数

?
1
2
3
4
<?= linkpager::widget([
  'pagination' => $pages,
  'maxbuttoncount' => 5,
]); ?>

有些人不喜欢默认的样式,想要分页带上自己的样式,可以设置options,不要忘了自行实现pre,next,disabled等样式

?
1
2
3
4
<?= linkpager::widget([
  'pagination' => $pages,
  'options' => ['class' => 'm-pagination'],
]); ?>

接下来我们谈谈pagination组件

默认的分页路由是下面这样子的,我们看看能做点什么

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

首先,我们是必须要指定总条数totalcount的,没这个参数,分页也是没办法实现的

?
1
2
3
$pages = new pagination([
  'totalcount' => $totalcount,
]);

默认分页的数量是20,你可以设置pagesize为你想要的

?
1
2
3
4
$pages = new pagination([
  'totalcount' => $totalcount,
  'pagesize' => 5,
]);

从上面的分页路由我们可以看到,默认带的有每页的数量per-page 如果你不想显示该参数,设置pagesizeparam=false就好

?
1
2
3
4
$pages = new pagination([
  'totalcount' => $totalcount,
  'pagesizeparam' => false,
]);

我们也可以看到,默认的页面取决于参数page,如果你想改变该参数为p,设置pageparam=p就好

?
1
2
3
4
$pages = new pagination([
  'totalcount' => $totalcount,
  'pageparam' => 'p',
]);

如果你的分页存在于首页,相信你肯定想要/?p=1而不是/site/index?p=1,我们看看怎么隐藏掉路由

?
1
2
3
4
$pages = new pagination([
  'totalcount' => $totalcount,
  'route' => false,
]);

可能你会发现分页类pagination有一个bug,假如我们只有1页的数据,但是手动更改地址栏的page=20的时候,也会显示page=1的数据?当然,这在大部分接口api中就很让人厌烦。但是,这并非bug,而是一种友好的验证。设置validatepage=false即可避免掉该问题

?
1
2
3
$pages = new pagination([
  'totalcount' => $totalcount,
  'validatepage' => false, ]);

最后,我们整点新花样,扩展下他这个自带的分页!别一看见扩展俩字下面的就直接不看了,只有自己学会扩展了,以后才能越来越强!怎么个扩展法呢?我们把分页组件改为上下页那种,具体参考下图做个对比吧

Yii2分页的使用及其扩展方法详解

接下来我们就来看看右侧的效果具体是如何通过扩展linkpager组件实现的。源码分享给大家,喜欢的拿去自己研究即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace frontend\components;
use yii\widgets\linkpager;
use yii\helpers\html;
class mlinkpager extends linkpager
{
  public $prevpagelabel = '<i class="fa fa-angle-left"></i>';
  public $nextpagelabel = '<i class="fa fa-angle-right"></i>';
  public $currentcountpagelabel = '第 {currentpage} 页 / 共 {countpage} 页';
  public $currentcountpageclass = 'page-number';
  public $hideonsinglepage = false;
  public function init () {
    parent::init();
  }
  public function run () {
    $pagecount = $this->pagination->getpagecount();
    if ($pagecount < 2 && $this->hideonsinglepage) {
      return '';
    }
    $buttons = [];
    $currentpage = $this->pagination->getpage();
    // prev page
    if ($this->prevpagelabel !== false) {
      if (($page = $currentpage - 1) < 0) {
        $page = 0;
      }
      $buttons[] = $this->renderpagebutton($this->prevpagelabel, $page, $this->prevpagecssclass, $currentpage <= 0, false);
    }
    // current page / count page
    if ($this->currentcountpagelabel !== false && $pagecount) {
      $currentcountpagelabel = str_replace(['{currentpage}', '{countpage}'], [$currentpage+1, $pagecount], $this->currentcountpagelabel);
      $buttons[] = html::tag('span', $currentcountpagelabel, array('class' => $this->currentcountpageclass));
    }
    // next page
    if ($this->nextpagelabel !== false) {
      if (($page = $currentpage + 1) >= $pagecount - 1) {
        $page = $pagecount - 1;
      }
      $buttons[] = $this->renderpagebutton($this->nextpagelabel, $page, $this->nextpagecssclass, $currentpage >= $pagecount - 1, false);
    }
    return html::tag('nav', implode("\n", $buttons), $this->options);
  }
  protected function renderpagebutton($label, $page, $class, $disabled, $active)
  {
    $options = ['class' => empty($class) ? $this->pagecssclass : $class];
    if ($active) {
      html::addcssclass($options, $this->activepagecssclass);
    }
    if ($disabled) {
      return false;
    }
    $linkoptions = $this->linkoptions;
    $linkoptions += $options;
    $linkoptions['data-page'] = $page;
    return html::a($label, $this->pagination->createurl($page), $linkoptions);
  }
}

如此一来,我们调用mlinkpager实现分页效果像下面这样即可

?
1
2
3
4
use frontend\components\mlinkpager;
<?= mlinkpager::widget([
  'pagination' => $pages,
]); ?>

当然,自己扩展的分页组建重在教大家如何去实现分页扩展,难免会有很多问题,如果你有好的意见或者方法,直接给我留言,咱们共同沟通交流。

延伸 · 阅读

精彩推荐