php 分页类 bootstrap,分享thinkphp5自定义Bootstrap分页类

分享thinkphp5.0.11的自定义Bootstrap分页类,tp5自带分页类Bootstrap.不能满足我的分页需求。

自带分页:

1、页码导航没有首页和末页

2、自带导航没有总记录数、总页数和当前页。如:共36条记录 当前第1/3页。

thinkphp5框架设计的很好,如果没有合适的,可以于驱动插件的形式自由自定义,只需改配置成自己定义的类就好了。

先看下最终效果:

本站的分页导航就是采用这个类实现的,如需体验请步至任何数量较多的列表页进行体验。

ba2075c03a451ab4f53700cbe5fa79b9.png

后双箭头:首页(可以自定义)

后单箭头:上一页(可以自定义)

中间数字:页码 (默认)

前单箭头:下一页(可以自定义)

前双箭头:末页(可以自定义)

总页数记录数等 (可以自定义)

第一步

进入:\thinkphp\library\think\paginator\driver

重写:Bootstrap.php类

新建:MyBootstrap.php文件<?php

namespace think\paginator\driver;

/**

* 自定义Bootstrap分页

* @author weizhixi.com

*/

class MyBootstrap extends Bootstrap{

/**

* 首页

* @param string $text 自定义显示文字

* @return string

*/

protected function getFirstButton($text = "«"){

if ($this->currentPage() <= 1) {

return $this->getDisabledTextWrapper($text);

}

$url = $this->url(1);

return $this->getPageLinkWrapper($url, $text);

}

/**

* 末页

* @param string $text 自定义显示文字

* @return string

*/

protected function getLastButton($text = "»"){

if (!$this->hasMore) {

return $this->getDisabledTextWrapper($text);

}

$url = $this->url($this->lastPage());

return $this->getPageLinkWrapper($url, $text);

}

/**

* 渲染分页html

* @return mixed

*/

public function render(){

if ($this->hasPages()) {

if ($this->simple) {

return sprintf(

'

  • %s %s %s %s
',

$this->getFirstButton("«"),

$this->getPreviousButton("‹"),

$this->getNextButton("›"),

$this->getLastButton("»")

);

} else {

return sprintf(

'

  • %s %s %s %s %s

共%s条记录 当前第%s/%s页

',

$this->getFirstButton("«"),

$this->getPreviousButton("‹"),

$this->getLinks(),

$this->getNextButton("›"),

$this->getLastButton("»"),

$this->total(),

$this->currentPage(),

$this->lastPage()

);

}

}else{

if ($this->simple) {

} else {

return sprintf(

'

共%s条记录 当前第%s/%s页

',

$this->total(),

$this->currentPage(),

$this->lastPage()

);

}

}

}

public function toArray(){

try {

$total = $this->total();

} catch (\DomainException $e) {

$total = null;

}

return [

'total'        => $total,

'last_page'    => $this->lastPage(),

'per_page'     => $this->listRows(),

'current_page' => $this->currentPage(),

'data'         => $this->items->toArray(),

'render'       => $this->render()

];

}

}

这里只要重写了:

getFirstButton() 自定义首页按钮

getLastButton() 自定义末页按钮

render() 渲染html

toArray() 这里我只是为了转json还有页码样式render。不用可删掉。

第二步

修改配置Config.php//分页配置

'paginate'               => [

'type'      => 'MyBootstrap',

'var_page'  => 'page',

'list_rows' => 15,

],

至此,配置完成。

如果自定义放于放于自己的项目里边,例如放在:application/common/util 下话。

那么配置改为://分页配置

'paginate'               => [

'type'      => 'app\common\util\MyBootstrap',

'var_page'  => 'page',

'list_rows' => 15,

],

也是一样的,在你喜欢。注意命名空间。

为什么可以这样,看源码片段:

\thinkphp\library\think\db\Query.php...

/** @var Paginator $class */

$class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']);

$page  = isset($config['page']) ? (int) $config['page'] : call_user_func([

$class,

'getCurrentPage',

], $config['var_page']);

...

可以看到type可以定义路径,无配置路径,就去driver找。

原创文章,转载请注明出处:https://www.weizhixi.com/article/37.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值