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 调用 ...
随机推荐
- 面向对象 【类库】【委托】【is as运算符】
类库(Class Library) .dll文件 类库字面意思就是类的集合,里面有很多被编译后的C#代码,不可阅读,不可修改,只能调用 类库是一个综合性的面向对象的可重用类型集合,这些类型包括:接口. ...
- Java基础——数组
一.大数据 如果基本的整型和浮点型精度不能够满足需求,那么可以使用java.math包含中的两个类:BigInteger和BigDecimal. 这两个类处理包含任意长度数字序列的数值.BigInte ...
- 数据库概念:码 键 Key & 范式 Normal Form
参考资料 数据库管理系统原理与设计(Database Mangement System 3rd) 百度 wiki 术语对照 码 = 键 = Key 码约束 = Key Constraints 码约束 ...
- python-享元模式
源码地址:https://github.com/weilanhanf/PythonDesignPatterns 说明: 如果一个软件系统在运行时所创建的相同或相似对象数量太多,将导致运行代价过高,带来 ...
- JS 作用域 p1
引用<你不知道的JavaScript>中的话,如下: 负责收集并维护由所有生命的标识符(变量)组成的一系列查询并实施一套非常严格的规则,确定当前执行的代码对这些标识符的访问权限. 那么作用 ...
- php获取本月、上月、上上月、今日、昨日、上周的起始时间
// 本月起始时间: $begin_time = date ( "Y-m-d H:i:s", mktime ( 0, 0, 0, date ( "m" ), 1 ...
- python解释器介绍以及Pycharm的破解
python语言是弱类型解释型语言,弱类型指的是没有强制规定它的类型. 由于是解释型语言,则必有解释器与其匹配,根据不同的工作环境以及需求,python的解释器有很多种, 官方推荐的是CPython, ...
- nginx禁止ip默认参数是$remote_addr无法禁止真实ip的问题
由于网站使用了cdn所以$remote_addr获取的ip是cdn的ip,我现在先禁止某些ip访问发现无法禁止cdn传递过来的客户端的ip也就是$http_x_forwarded_for这个参数.比如 ...
- git cherry-pick 用法
1.当合并代码冲突特别多的时候,有时候只想提交自己分支的代码.这个时候使用cherry-pick 可以实现 1)首先使用 git log --oneline -n 找到最近自己分支的提交记录,n表示提 ...
- 关于npm run build打包后css样式中的图片失效的问题(如background)
平时run dev都能正常显示的css背景图片在npm run build打包后竟然不显示了(写在标签对中的图片都可以正常显示),而且dist/static/img目录下是确实有这张图片的,于是查看打 ...