结合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 分页类,自定义样式的更多相关文章

  1. TP5分页类

    <?php class Page { public $page; //当前页 public $total; //总记录数 public $listRows; //每页显示记录数 private ...

  2. php分页类代码带分页样式效果(转)

    php分页类代码,有漂亮的分页样式风格 时间:2016-03-16 09:16:03来源:网络 导读:不错的php分页类代码,将类文件与分页样式嵌入,实现php查询结果的精美分页,对研究php分页原理 ...

  3. 好用的ASP.NET 分页类 简单好用 支持 AJAX 自定义文字

    在做网站没用 JS UI控件时 很实用 用法: var ps=new PageString(); /*可选参数*/ ps.SetIsEnglish = true;// 是否是英文 (默认:false) ...

  4. ThinkPHP3验证码、文件上传、缩略图、分页(自定义工具类、session和cookie)

    验证码 TP框架中自带了验证码类 位置:Think/verify.class.php 在LoginController控制器中创建生存验证码的方法 login.html登陆模板中 在LoginCont ...

  5. 自己实现的数据表格控件(dataTable),支持自定义样式和标题数据、ajax等各种自定义设置以及分页自定义

    一.前言 也没什么好说的嘛,用了蛮多github上开源的能够实现dataTable功能的表格插件,不过都默认绑定样式啊,数据格式也设定的比较死,所以忍不住自己实现了一个简单的可自定义样式和自定义数据返 ...

  6. thinkphp自定义分页类

    先来看下这个分页的样式,没写css,确实丑 什么时候写样式再来上传下css吧...... 就是多一个页面跳转功能 先把这个代码贴一下 <?php namespace Component; cla ...

  7. Django 自定义分页类

    分页类代码: class Page(object): ''' 自定义分页类 可以实现Django ORM数据的的分页展示 输出HTML代码: 使用说明: from utils import mypag ...

  8. 自定义MVC框架之工具类-分页类的封装

    以前写过一个MVC框架,封装的有点low,经过一段时间的沉淀,打算重新改造下,之前这篇文章封装过一个验证码类. 这次重新改造MVC有几个很大的收获 >全部代码都是用Ubuntu+Vim编写,以前 ...

  9. PHP24 自定义分页类

    分页类的定义 <?php /** * Class MyPage 分页类 * @package core */ class MyPage { private $totalCount; //数据表中 ...

随机推荐

  1. 多年js学习累计总结

    http://www.codesec.net/list/6/ 大神http://www.cnblogs.com/tylerdonet/p/5543813.html

  2. 理解UIView的绘制-孙亚洲

    前言 最近研究OpenGL ES相关和 GPU 相关 发现这篇文章很具有参考的入门价值. 理解 UIView 的绘制, UIView 是如何显示到 Screen 上的? 首先要从Runloop开始说, ...

  3. python简单的购物系统

    #coding = utf-8 #2016-11-19#我的工资是存在文件中的,执行后会判断是否存过工资,如果存过无需输入,直接购物,没存过需要输入工资#wages.txt是存工资的文件 import ...

  4. 关于表格元素的使用,table、<width>、<heigh>、<border>、<tr>、<th>、<td>、<align>、<colspan>、<rowspan>

    <html>    <head>        <meta charset="UTF-8">        <title>个人简历& ...

  5. luoguP5055 【模板】可持久化文艺平衡树 可持久化非旋转treap

    好题. Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s".in&quo ...

  6. 【洛谷4941】War2 状压Dp

    简单的状压DP,和NOIP2017 Day2 找宝藏 代码几乎一样.(比那个稍微简单一点) f[i][j] ,i代表点的状态,j是当前选择的点,枚举上一个选到的点k 然后从f[i-(1<< ...

  7. UVA401-Palindromes(紫书例题3.3)

    A regular palindrome is a string of numbers or letters that is the same forward as backward. For exa ...

  8. pip常见用法汇总

    1.pip安装 yum -y install epel-release && yum -y install python-pip 2.pip安装软件 (1)安装单个软件:pip ins ...

  9. Linux之awk使用

    基本语法 $n :当前记录的第n个字段,比如n为1表示第一个字段,n为2表示第二个字段 $0:执行过程中当前行的文本内容 \t:制表符 \n:换行符 -F'[:#/]' : 定义三个分隔符,注意有-F ...

  10. Layui Excle/csv数据导出

    官方文档的数据是这样的 依赖 Layui 2.4版本以上 layui.use([ 'table'], function(){ var table=layui.table; table.exportFi ...