thinkphp自定义分页组件
在开发过程中可能会遇到一些不是从数据库查询的数据需要分页的,这个时候就需要自己写分页了,下边是一个可自定义的分页方法
示例接口
public function lst()
{
$page = isset($param['page']) && $param['page'] ? $param['page'] : 1;// 当前第几页
$pageSize = isset($param['pageSize']) && $param['pageSize'] ? $param['pageSize'] : 5;// 每页条数
$list = [
['id'=>1,'name'=>'一一一'],
['id'=>2,'name'=>'二二二'],
['id'=>3,'name'=>'三三三'],
['id'=>4,'name'=>'四四四'],
['id'=>5,'name'=>'五五五'],
['id'=>6,'name'=>'六六六'],
['id'=>7,'name'=>'七七七'],
['id'=>8,'name'=>'八八八'],
['id'=>9,'name'=>'九九九'],
['id'=>10,'name'=>'十十十'],
['id'=>11,'name'=>'十一十一十一'],
['id'=>12,'name'=>'十二十二十二'],
];
// 排序
$sort = array_column($list,'id');
array_multisort($sort, SORT_DESC, $list);
$dataTo = array_chunk($list, $pageSize);// 分割数组
if($dataTo){
$showData = $dataTo[$page-1] ?? null;
}else{
$showData = null;
}
$list = new Page($showData, $pageSize, $page, count($list), false, []);
$this->success('', $list);
}
返回结果
{
"code": 1,
"msg": "",
"time": "",
"data": {
"total": 12,
"per_page": 5,
"current_page": 1,
"last_page": 3,
"data": [
{
"id": 12,
"name": "十二十二十二"
},
{
"id": 11,
"name": "十一十一十一"
},
{
"id": 10,
"name": "十十十"
},
{
"id": 9,
"name": "九九九"
},
{
"id": 8,
"name": "八八八"
}
]
}
}
分页方法,可用于后台管理,自定义样式
<?php
/**
* 自定义分页
*/
namespace page;
use think\Paginator;
class Page 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()
{
return "<li class=\"disabled\"><span>第".$this->currentPage()."页/共".$this->lastPage()."页</span></li>";
}
/**
* 尾页标签
* @param string $text
* @return string
*/
protected function showLastPage($text = '尾页')
{
if($this->currentPage()==$this->lastPage())
{
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->lastPage());
return $this->getPageLinkWrapper($url, $text);
}
/**
* 首页标签
* @param string $text
* @return string
*/
protected function showFirstPage($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 aFivePage($text = '后五页')
{
if($this->lastPage()<$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 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 "<li><form style='float: left' action='' method='get' ><a style='float:left;margin-left:2px;'>
<input type='hidden' name='[NAME]' value='[VALUE]'>
<input style='width: 90px;height: 30px;border: 1px solid #C7C7C7;border-radius: 3px' type='text' name='page' placeholder='请输入页码'>
<input class='btn btn-sm btn-default' type='submit' value='确定'> </a></form></li>";
}
/**
* 页码按钮
* @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
* @param string $name 跳转页码参数名
* @param string $value 跳转页码参数值
* @return mixed
*/
public function render($name='',$value='')
{
if ($this->hasPages()) {
if ($this->simple) {
return sprintf(
'<ul class="pager">%s %s %s</ul>',
$this->getPreviousButton(),
$this->getNextButton()
);
} else {
$goPage = $this->goPage();
$goPage = str_replace('[NAME]', $name, $goPage);
$goPage = str_replace('[VALUE]', $value, $goPage);
return sprintf(
'<ul class="pagination pagination-sm no-margin pull-right"> %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 可以显示跳转到哪页
$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()
{
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);
}
}