Thinkphp5.0分页和跳页
后台查询商品或者会员量需要用到分页展示列表,当页数比较多的时候为了体高用户体验度,需要添加一个跳页也就是手动输入页码数进行快速跳转指定页面。由于手动编写分页比较麻烦,又想使用TP5自带的分页,但是TP5自带的分页类比较简单,所以可以通过修改Bootstrap类自定义显示分页的页码和数量。
由于Bootstrap类是tp自带的类,所以为了我们尽量不要改动底层自带的类,这里拷贝一下Bootstrap类然后重命名为BootstrapDetailed.php,目录结构如图:
代码如下:
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: zhangyajun <448901948@qq.com>
- // +----------------------------------------------------------------------
- namespace think\paginator\driver;
- use think\Paginator;
- class BootstrapDetailed extends Paginator
- {
- /**
- * 上一页按钮
- * @param string $text
- * @return string
- */
- protected function getPreviousButton($text = "上一页")
- {
- if ($this->currentPage() <= 1) {
- return $this->getDisabledTextWrapper($text);
- }
- $url = $this->url(
- $this->currentPage() - 1
- );
- return $this->getPageLinkWrapper($url, $text);
- }
- //总数标签
- protected function totalshow()
- {
- $totalhtml="<li class=\"disabled\"><span>共".$this->total."条记录  第".$this->currentPage()."页/共".$this->lastPage()."页</span></li>";
- return $totalhtml;
- }
- //尾页标签
- protected function showlastpage($text = '尾页')
- {
- if($this->currentPage()==$this->lastPage())
- {
- return $this->getDisabledTextWrapper($text);
- }
- $url = $this->url($this->lastPage());
- return $this->getPageLinkWrapper($url, $text);
- }
- //首页标签
- protected function showfirstpage($text = '首页')
- {
- if($this->currentPage()==1)
- {
- return $this->getDisabledTextWrapper($text);
- }
- $url = $this->url(1);
- return $this->getPageLinkWrapper($url, $text);
- }
- //后五页
- protected function afivepage($text = '后五页')
- {
- if($this->lastPage()<$this->currentPage()+5)
- {
- return $this->getDisabledTextWrapper($text);
- }
- $url = $this->url($this->currentPage()+5);
- return $this->getPageLinkWrapper($url, $text);
- }
- //前五页
- protected function bfivepage($text = '前五页')
- {
- if($this->currentPage()<5)
- {
- return $this->getDisabledTextWrapper($text);
- }
- $url = $this->url($this->currentPage()-5);
- return $this->getPageLinkWrapper($url, $text);
- }
- /**
- * 下一页按钮
- * @param string $text
- * @return string
- */
- protected function getNextButton($text = '下一页')
- {
- if (!$this->hasMore) {
- return $this->getDisabledTextWrapper($text);
- }
- $url = $this->url($this->currentPage() + 1);
- return $this->getPageLinkWrapper($url, $text);
- }
- //跳转到哪页
- protected function gopage()
- {
- return $gotohtml="<li><form action='' method='get' ><a style='float:left;margin-left:2px;'><input style='height:33px;' type='text' name='page' placeholder='请选择页数'> <input style='height:33px;' type='submit' value='确定'> </a></form></li>";
- // return $totalhtml;;
- }
- /**
- * 页码按钮
- * @return string
- */
- protected function getLinks()
- {
- if ($this->simple)
- return '';
- $block = [
- 'first' => null,
- 'slider' => null,
- 'last' => null
- ];
- $side = 2;
- $window = $side * 2;
- if ($this->lastPage < $window +1) {
- $block['slider'] = $this->getUrlRange(1, $this->lastPage);
- } elseif ($this->currentPage <= $window-1) {
- $block['slider'] = $this->getUrlRange(1, $window + 1);
- } elseif ($this->currentPage > ($this->lastPage - $window+1)) {
- $block['slider'] = $this->getUrlRange($this->lastPage - ($window), $this->lastPage);
- } else {
- $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
- }
- $html = '';
- if (is_array($block['first'])) {
- $html .= $this->getUrlLinks($block['first']);
- }
- if (is_array($block['slider'])) {
- $html .= $this->getUrlLinks($block['slider']);
- }
- if (is_array($block['last'])) {
- $html .= $this->getUrlLinks($block['last']);
- }
- return $html;
- }
- /**
- * 渲染分页html
- * @return mixed
- */
- public function render()
- {
- if ($this->hasPages()) {
- if ($this->simple) {
- return sprintf(
- '<ul class="pager">%s %s %s</ul>',
- $this->getPreviousButton(),
- $this->getNextButton()
- );
- } else {
- return sprintf(
- '<ul class="pagination"> %s %s %s %s %s %s %s %s </ul>',
- //显示数量页码信息
- $this->totalshow(),
- //第一页
- $this->showfirstpage(),
- //上一页
- $this->getPreviousButton(),
- //前五页
- $this->bfivepage(),
- //页码
- $this->getLinks(),
- //后五页
- //$this->afivepage(),
- //下一页
- $this->getNextButton(),
- //最后一页
- $this->showlastpage(),
- //最后再加个参数 %s 可以显示跳转到哪页
- $this->gopage()
- );
- }
- }
- }
- /**
- * 生成一个可点击的按钮
- *
- * @param string $url
- * @param int $page
- * @return string
- */
- protected function getAvailablePageWrapper($url, $page)
- {
- return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
- }
- /**
- * 生成一个禁用的按钮
- *
- * @param string $text
- * @return string
- */
- protected function getDisabledTextWrapper($text)
- {
- return '<li class="disabled"><span>' . $text . '</span></li>';
- }
- /**
- * 生成一个激活的按钮
- *
- * @param string $text
- * @return string
- */
- protected function getActivePageWrapper($text)
- {
- return '<li class="active"><span>' . $text . '</span></li>';
- }
- /**
- * 生成省略号按钮
- *
- * @return string
- */
- protected function getDots($text = '...')
- {
- //$url = $this->url($this->currentPage() + 1);
- // return $this->getPageLinkWrapper($url, $text);
- return $this->getDisabledTextWrapper('...');
- }
- /**
- * 批量生成页码按钮.
- *
- * @param array $urls
- * @return string
- */
- protected function getUrlLinks(array $urls)
- {
- $html = '';
- foreach ($urls as $page => $url) {
- $html .= $this->getPageLinkWrapper($url, $page);
- }
- return $html;
- }
- /**
- * 生成普通页码按钮
- *
- * @param string $url
- * @param int $page
- * @return string
- */
- protected function getPageLinkWrapper($url, $page)
- {
- if ($page == $this->currentPage()) {
- return $this->getActivePageWrapper($page);
- }
- return $this->getAvailablePageWrapper($url, $page);
- }
- }
然后再config里面配置一下分页类就可以了。
- //分页配置
- 'paginate' => [
- 'type' => 'BootstrapDetailed',
- 'var_page' => 'page',
- 'list_rows' => 15,
- ],
举个例子:
- $good_info =Db::table('goods')->paginate(20);
效果如下:
这样就可以了,用户体验度高,方便。很不错的分页。
原文参考文章地址:http://www.zhaisui.com/article/52.html
Thinkphp5.0分页和跳页的更多相关文章
- thinkPHP5.0分页传参
分页函数paginate(),主要参数有:list_rows每页数量.page当前页.path URL路径.query URL额外参数.fragment URL锚点.type分页l类型 public ...
- thinkphp5.0分页
第一种 public function index(){ // 页面和面包屑导航 $ttl[] = $this->title; $ttl[] = '管理员列表'; $this->assig ...
- thinkphp5.0 分页中伪静态的处理
1.修改文件\thinkphp\library\think\Paginator.php(此文件用于分页) isurl是否为伪静态 加入isurl用于判断是否使用伪静态分页 */ protected $ ...
- 用Vue2.0实现简单的分页及跳转
用Vue2.0实现简单的分页及跳转 2018年07月26日 20:29:51 Freya_yyy 阅读数 3369 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...
- ThinkPHP5.0框架开发实现简单的页面跳转
ThinkPHP5.0框架开发实现简单的页面跳转 一.效果 登录界面 登录成功界面 登录失败界面 二.目录结构 三.代码 控制器中的Login.php <?php // 声明命名空间 names ...
- Thinkphp5——实现分页(模型和Db分页,多种方法)
现在很多网站的数据量的很多,如果全部在一页里显示效果不好,数据量太大,那怎么办?这时我们就需要分页,而分页的好处就是分段显示数据,这样页面就不用加载很多数据,需要时才加载,下面我教大家实现ThinkP ...
- thinkphp5.0 微信公众号接入支付宝支付
---恢复内容开始--- 真是无力吐槽这个需求了,想骂客户,好端端的非要在微信公众号接入支付宝,都知道微信公众号是拒绝支付宝的,屏蔽了支付宝,所以在微信公众号接入支付宝的话就必须手动复制链接跳出微信内 ...
- mvc自定义分页(加页数的)(转)
1.引言 在MVC开发中我们经常会对数据进行分页的展示.通过分页我们可以从服务端获取指定的数据来进行展示.这样既节约了数据库查询的时间也节约了网络传输的数据量.在MVC开发中使用的比较多的应该是MVC ...
- Thinkphp5.0支付宝支付扩展库类库大全
Thinkphp5.0支付宝支付扩展库类库大全,包括手机网站支付.电脑网站支付.支付查询.退款.退款查询.对账单等. Thinkphp5.0支付宝调用方法: 电脑网站支付 Pagepay.php 调用 ...
随机推荐
- [EWS]如何: 通过使用 Exchange 中的 EWS 流有关邮箱事件的通知
摘要 在之前的文章中,介绍ews拉通知的模式订阅邮件.详情可阅读这篇文章:EWS 通过SubscribeToPullNotifications订阅Exchange新邮件提醒 ,可以看到拉通知的模式,是 ...
- Redis——非阻塞IO和队列
Redis是个高并发的中间件,但是确实是单线程.而且,Nginx.Node.js等也是单线程的.Redis通过非阻塞IO(IO多路复用)处理那么多的并发客户端连接,并且,由于Redis所有的数据都在内 ...
- mongodb在线web管理工具
随着云计算,大数据等技术的不断发展,需要服务应用都朝着网络化,在线化的方向演进,数据库管理,数据库维护,数据可视化等也是这种趋势.MonggoDB,MySQL的在线管理,已成为一种强烈的需求,使用Tr ...
- Android - 注解
原理: http://www.cnblogs.com/Fndroid/p/5354644.html http://www.jianshu.com/p/28edf5352b63 开源库: ButterK ...
- Mycat入门配置_读写分离配置
1.Mycat的分片 两台数据库服务器: 192.168.80.11 192.168.80.4 操作系统版本环境:centos6.5 数据库版本:5.6 mycat版本:1.4 release 数据库 ...
- 在学习使用webpack中遇到的错误
1.局部安装webpack之后使用node_modules/.bin/webpack app/main.js public/bundle.js命令报错 解决方法: webpack版 ...
- Nginx控制并发连接数
ngx_http_limit_conn_module这个模块用于限制每个定义的key值的连接数,特别是单IP的连接数. 不是所有的连接数都会被计数.一个符合计数要求的连接是整个请求头已经被读取的连接. ...
- 简单的分页小demo
public class Demo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Sy ...
- Python+Selenium笔记(十六)屏幕截图
(一) 方法 方法 简单说明 save_screenshot(filename) 获取当前屏幕截图并保存为指定文件 filename:路径/文件名 get_screenshot_as_base64 ...
- 机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理、源码解析及测试
机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理.源码解析及测试 关键字:决策树.python.源码解析.测试作者:米仓山下时间:2018-10-2 ...