Home > php教程 > php手册 > yii2分页的使用及其扩展

yii2分页的使用及其扩展

WBOY
Release: 2016-05-25 18:02:53
Original
1267 people have browsed it

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

  • 分页的使用,一步一步的教你怎么做
  • 分页类LinkPager和Pagination都可以自定义哪些属性
  • 分页类LinkPager如何扩展成我们所需要的

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

1、controller action

<span style="color: #0000ff;">use</span><span style="color: #000000;"> yii\data\Pagination;
</span><span style="color: #800080;">$query</span> = Article::find()->where(['status' => 1<span style="color: #000000;">]);
</span><span style="color: #800080;">$countQuery</span> = <span style="color: #0000ff;">clone</span> <span style="color: #800080;">$query</span><span style="color: #000000;">;
</span><span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span> Pagination(['totalCount' => <span style="color: #800080;">$countQuery</span>-><span style="color: #008080;">count</span><span style="color: #000000;">()]);
</span><span style="color: #800080;">$models</span> = <span style="color: #800080;">$query</span>->offset(<span style="color: #800080;">$pages</span>-><span style="color: #000000;">offset)
    </span>->limit(<span style="color: #800080;">$pages</span>-><span style="color: #000000;">limit)
    </span>-><span style="color: #000000;">all();
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->render('index',<span style="color: #000000;"> [
    </span>'models' => <span style="color: #800080;">$models</span>,
    'pages' => <span style="color: #800080;">$pages</span>,<span style="color: #000000;">
]);</span>
Copy after login

 

2、View

<span style="color: #0000ff;">use</span><span style="color: #000000;"> yii\widgets\LinkPager;
</span><span style="color: #008000;">//</span><span style="color: #008000;">循环展示数据</span>
<span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$models</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$model</span><span style="color: #000000;">) {
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> ......</span>
<span style="color: #000000;">}
</span><span style="color: #008000;">//</span><span style="color: #008000;">显示分页页码</span>
<span style="color: #0000ff;">echo</span> LinkPager::<span style="color: #000000;">widget([
    </span>'pagination' => <span style="color: #800080;">$pages</span>,<span style="color: #000000;">
])</span>
Copy after login

 

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

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

 

首先我们说说LinkPager组件

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

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

  • 上下页按钮以及10个按钮
  • 首先,我们把上下页的按钮修改成中文
= LinkPager::<span style="color: #000000;">widget([ 
    </span>'pagination' => <span style="color: #800080;">$pages</span>, 
    'nextPageLabel' => '下一页', 
    'prevPageLabel' => '上一页',<span style="color: #000000;"> 
]); </span>?>
Copy after login

 

  • 如果你不想要显示上下页,可以将prevPageLabel和nextPageLabel设置为false
= LinkPager::<span style="color: #000000;">widget([ 
    </span>'pagination' => <span style="color: #800080;">$pages</span>, 
    'nextPageLabel' => <span style="color: #0000ff;">false</span>, 
    'prevPageLabel' => <span style="color: #0000ff;">false</span>,<span style="color: #000000;"> 
]); </span>?>
Copy after login

 

  • 默认不显示首页也尾页,如果你需要,可以这样设置
= LinkPager::<span style="color: #000000;">widget([ 
    </span>'pagination' => <span style="color: #800080;">$pages</span>, 
    'firstPageLabel' => '首页', 
    'lastPageLabel' => '尾页',<span style="color: #000000;"> 
]); </span>?>
Copy after login

 

  • 如果你的数据过少,不够2页,默认不显示分页,如果你需要,设置hideOnSinglePage=false即可
= LinkPager::<span style="color: #000000;">widget([ 
    </span>'pagination' => <span style="color: #800080;">$pages</span>, 
    'hideOnSinglePage' => <span style="color: #0000ff;">false</span>,<span style="color: #000000;"> 
]); </span>?>
Copy after login

 

  • 默认显示的页码为10页,可以设置maxButtonCount为你想要展示的页数
= LinkPager::<span style="color: #000000;">widget([ 
    </span>'pagination' => <span style="color: #800080;">$pages</span>, 
    'maxButtonCount' => 5,<span style="color: #000000;"> 
]); </span>?>
Copy after login

 

  • 有些人不喜欢默认的样式,想要分页带上自己的样式,可以设置options,不要忘了自行实现pre,next,disabled等样式
= LinkPager::<span style="color: #000000;">widget([ 
    </span>'pagination' => <span style="color: #800080;">$pages</span>, 
    'options' => ['class' => 'm-pagination'],<span style="color: #000000;"> 
]); </span>?>
Copy after login

 

接下来我们谈谈Pagination组件

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

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

  • 首先,我们是必须要指定总条数totalCount的,没这个参数,分页也是没办法实现的
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ 
    </span>'totalCount' => <span style="color: #800080;">$totalCount</span>,<span style="color: #000000;"> 
]);</span>
Copy after login

 

  • 默认分页的数量是20,你可以设置pageSize为你想要的
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ 
    </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 
    'pageSize' => 5,<span style="color: #000000;"> 
]);</span>
Copy after login

 

  • 从上面的分页路由我们可以看到,默认带的有每页的数量per-page 如果你不想显示该参数,设置pageSizeParam=false就好
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ 
    </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 
    'pageSizeParam' => <span style="color: #0000ff;">false</span>,<span style="color: #000000;"> 
]);</span>
Copy after login

 

  • 我们也可以看到,默认的页面取决于参数page,如果你想改变该参数为p,设置pageParam=p就好
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ 
    </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 
    'pageParam' => 'p',<span style="color: #000000;"> 
]);</span>
Copy after login

 

  • 如果你的分页存在于首页,相信你肯定想要/?p=1而不是/site/index?p=1,我们看看怎么隐藏掉路由
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ 
    </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 
    'route' => <span style="color: #0000ff;">false</span>,<span style="color: #000000;"> 
]);</span>
Copy after login

 

  • 可能你会发现分页类Pagination有一个bug,假如我们只有1页的数据,但是手动更改地址栏的page=20的时候,也会显示page=1的数据?当然,这在大部分接口API中就很让人厌烦。但是,这并非bug,而是一种友好的验证。设置validatePage=false即可避免掉该问题
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ 
    </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 
    'validatePage' => <span style="color: #0000ff;">false</span>, ]);
Copy after login

 

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

​[考虑目前国内网站大部分采集文章十分频繁,更有甚者不注明原文出处,原作者更希望看客们查看原文,以防有任何问题不能更新所有文章,避免误导!]

继续阅读

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template