TP5 分页类,自定义样式
结合X-admin 后台框架 在做项目,为了保持分页风格的一致,所以需要自定义 一个分页类。
一、在项目的 extend 目录,创建 cus 目录
二、创建 Page 分页类,代码如下
<?php
namespace cus;
use think\Paginator;
///自定义样式分页类
class Page extends Paginator
{
//首页
protected function home() {
if ($this->currentPage() > 1) {
return "<a href='" . $this->url(1) . "' title='首页'>首页</a>";
} else {
return '';
}
}
//上一页
protected function prev() {
if ($this->currentPage() > 1) {
return "<a class='prev' href='" . $this->url($this->currentPage - 1) . "' title='上一页'>上一页</a>";
} else {
return '<span>上一页</span>';
}
}
//下一页
protected function next() {
if ($this->hasMore) {
return "<a class='next' href='" . $this->url($this->currentPage + 1) . "' title='下一页'>下一页</a>";
} else {
return '<span>下一页</span>';
}
}
//尾页
protected function last() {
if ($this->hasMore) {
return "<a href='" . $this->url($this->lastPage) . "' title='尾页'>尾页</a>";
} else {
return '';
}
}
//统计信息
protected function info(){
return "<p class='page-info'>共<b>" . $this->lastPage .
"</b>页 <b>" . $this->total . "</b>条数据</p>";
}
/**
* 页码按钮
* @return string
*/
protected function getLinks()
{
$block = [
'first' => null,
'slider' => null,
'last' => null
];
$side = 3;
$window = $side * 2;
if ($this->lastPage < $window + 6) {
$block['first'] = $this->getUrlRange(1, $this->lastPage);
} elseif ($this->currentPage <= $window) {
$block['first'] = $this->getUrlRange(1, $window + 2);
$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
} elseif ($this->currentPage > ($this->lastPage - $window)) {
$block['first'] = $this->getUrlRange(1, 2);
$block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
} else {
$block['first'] = $this->getUrlRange(1, 2);
$block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
}
$html = '';
if (is_array($block['first'])) {
$html .= $this->getUrlLinks($block['first']);
}
if (is_array($block['slider'])) {
$html .= $this->getDots();
$html .= $this->getUrlLinks($block['slider']);
}
if (is_array($block['last'])) {
$html .= $this->getDots();
$html .= $this->getUrlLinks($block['last']);
}
return $html;
}
/**
* 渲染分页html
* @return mixed
*/
public function render()
{
if ($this->hasPages()) {
if ($this->simple) {
return sprintf(
'<div class="page">%s %s %s</div>',
$this->prev(),
$this->getLinks(),
$this->next()
);
} else {
return sprintf(
'<div class="page">%s %s %s %s %s %s</div>',
$this->home(),
$this->prev(),
$this->getLinks(),
$this->next(),
$this->last(),
$this->info()
);
}
}
return '';
}
/**
* 生成一个可点击的按钮
*
* @param string $url
* @param int $page
* @return string
*/
protected function getAvailablePageWrapper($url, $page)
{
return '<a href="' . htmlentities($url) . '" title="第 '. $page .' 页" >' . $page . '</a>';
}
/**
* 生成一个禁用的按钮
*
* @param string $text
* @return string
*/
protected function getDisabledTextWrapper($text)
{
return '<span class="node-border">' .$text. '</span>';
}
/**
* 生成一个激活的按钮{当前页}
*
* @param string $text
* @return string
*/
protected function getActivePageWrapper($text)
{
return '<span class="current">' .$text. '</span>';
}
/**
* 为了样式美观生成空格按钮
*
* @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);
}
}
三、修改 config.php 配置如下:
//分页配置
'paginate' => [
'type' => 'cus\Page',
'var_page' => 'page',
'list_rows' => 15,
],
最终效果如下

其他风格可参考:http://www.thinkphp.cn/topic/50858.html
TP5 分页类,自定义样式的更多相关文章
- TP5分页类
<?php class Page { public $page; //当前页 public $total; //总记录数 public $listRows; //每页显示记录数 private ...
- php分页类代码带分页样式效果(转)
php分页类代码,有漂亮的分页样式风格 时间:2016-03-16 09:16:03来源:网络 导读:不错的php分页类代码,将类文件与分页样式嵌入,实现php查询结果的精美分页,对研究php分页原理 ...
- 好用的ASP.NET 分页类 简单好用 支持 AJAX 自定义文字
在做网站没用 JS UI控件时 很实用 用法: var ps=new PageString(); /*可选参数*/ ps.SetIsEnglish = true;// 是否是英文 (默认:false) ...
- ThinkPHP3验证码、文件上传、缩略图、分页(自定义工具类、session和cookie)
验证码 TP框架中自带了验证码类 位置:Think/verify.class.php 在LoginController控制器中创建生存验证码的方法 login.html登陆模板中 在LoginCont ...
- 自己实现的数据表格控件(dataTable),支持自定义样式和标题数据、ajax等各种自定义设置以及分页自定义
一.前言 也没什么好说的嘛,用了蛮多github上开源的能够实现dataTable功能的表格插件,不过都默认绑定样式啊,数据格式也设定的比较死,所以忍不住自己实现了一个简单的可自定义样式和自定义数据返 ...
- thinkphp自定义分页类
先来看下这个分页的样式,没写css,确实丑 什么时候写样式再来上传下css吧...... 就是多一个页面跳转功能 先把这个代码贴一下 <?php namespace Component; cla ...
- Django 自定义分页类
分页类代码: class Page(object): ''' 自定义分页类 可以实现Django ORM数据的的分页展示 输出HTML代码: 使用说明: from utils import mypag ...
- 自定义MVC框架之工具类-分页类的封装
以前写过一个MVC框架,封装的有点low,经过一段时间的沉淀,打算重新改造下,之前这篇文章封装过一个验证码类. 这次重新改造MVC有几个很大的收获 >全部代码都是用Ubuntu+Vim编写,以前 ...
- PHP24 自定义分页类
分页类的定义 <?php /** * Class MyPage 分页类 * @package core */ class MyPage { private $totalCount; //数据表中 ...
随机推荐
- where条件顺序与建索引顺序
查询时,如果数据量很大,where 后面的条件与建索引的顺序相同,也没有什么多少差别,聚集索引稍微快点; 但where 后面的条件与建索引顺序不同,速度会慢下来,到底慢多少,不同的机器会不一样,没有绝 ...
- POST和GET详解
GET和POST Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上 ...
- BZOJ 3697/3127 采药人的路径 (点分治)
题目大意: 从前有一棵无向树,树上边权均为$0$或$1$,有一个采药人,他认为如果一条路径上边权为$0$和$1$的边数量相等,那么这条路径阴阳平衡.他想寻找一条合法的采药路径,保证阴阳平衡.然后他发现 ...
- NOIP2013 华容道 (棋盘建图+spfa最短路)
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...
- $attr和$listeners is readonly
https://www.jb51.net/article/132371.htm 出现这个问题的原因,主要是因为在使用的时候出现了A组件调用B组件,B组件再调用了C组件.而直接使用了A组件修改C组件的数 ...
- 移动端rem自适应设置
对于移动端自适应各种终端的解决方案较多,本篇只是选择其中一种rem适配,我个人做移动端最喜欢的方案. rem就是以html根元素的字体大小为参考,比如html:font-size:20px;1rem= ...
- WinServer-IIS-压缩及缓存
静态内容压缩: 默认10s内有2个客户端一起请求服务器的话,服务器就会把相关的静态内容压缩返回 动态内容压缩: 默认IIS的程序域最高可以占用CPU90%的资源,这个可以通过命令行修改 缓存和内核缓存 ...
- BZOJ 2005 [Noi2010]能量採集 (容斥)
[Noi2010]能量採集 Time Limit: 10 Sec Memory Limit: 552 MB Submit: 2324 Solved: 1387 [id=2005"> ...
- 一个ibatis映射文件的例子(包含增删改单查,多查)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...
- Stop being a perfectionist
节选自 7 Things You Need To Stop Doing To Be More Productive, Backed By Science “We found that perfecti ...