thinkphp5 layui分页样式[通俗易懂]

thinkphp5 layui分页样式[通俗易懂]tp5之layui分页样式1.分页类路径:\thinkphp\library\think\paginator\driverLayui.php<?phpnamespacethink\paginator\driver;usethink\Paginator;classLayuiextendsPaginator{/***上一页按钮…

大家好,又见面了,我是你们的朋友全栈君。

tp5之layui分页样式

1.分页类

路径:\thinkphp\library\think\paginator\driver

Layui.php

<?php
namespace think\paginator\driver;
use think\Paginator;
class Layui 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);
    }

    /**
     * 下一页按钮
     * @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);
    }

    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks()
    {
        if ($this->simple)
            return '';

        $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(
                    '<ul class="pager">%s %s</ul>',
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    '%s %s %s',
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton()
                );
            }
        }
    }

    /**
     * 生成一个可点击的按钮
     *
     * @param  string $url
     * @param  int    $page
     * @return string
     */
    protected function getAvailablePageWrapper($url, $page)
    {
        return '<a href="' . htmlentities($url) . '">' . $page . '</a>';
    }

    /**
     * 生成一个禁用的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getDisabledTextWrapper($text)
    {
        return '<a class="layui-laypage-prev" >' . $text . '</a>';
    }

    /**
     * 生成一个激活的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getActivePageWrapper($text)
    {
        return '<span class="layui-laypage-curr"> <em class="layui-laypage-em"></em><em>' . $text . '</em></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);
    }
}

2.配置文件

paginate.php

<?php
/**
 * @auther: xxf
 * Date: 2019/9/2
 * Time: 10:24
 */
//分页配置
return [
    'type' => 'Layui',
    'var_page' => 'page',
];

3.模型查询

 public function getUserShowList($size = 20, $where = null)
    {
        $res = $this
            ->field('id,title,list_order,is_top,create_time,create_time time')
            ->where($where)
            ->order(['is_top' => 'desc', 'list_order' => 'desc', 'id' => 'desc'])
            ->paginate($size);
      
        return $res;
    }

4.模板渲染

<div class="layui-box layui-laypage layui-laypage-molv">{$list|raw}</div>

效果

thinkphp5 layui分页样式[通俗易懂]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160660.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • oracle ebs 12.20 安装成功其过程失败日记及总结(1)

    oracle ebs 12.20 安装成功其过程失败日记及总结(1)

    2021年12月15日
    45
  • CGLIB(Code Generation Library)详解[通俗易懂]

    CGLIB(Code Generation Library)详解[通俗易懂]什么是CGLIBCGLIB是一个强大的、高性能的代码生成库。其被广泛应用于AOP框架(Spring、dynaop)中,用以提供方法拦截操作。Hibernate作为一个比较受欢迎的ORM框架,同样使用CGLIB来代理单端(多对一和一对一)关联(延迟提取集合使用的另一种机制)。CGLIB作为一个开源项目,其代码托管在github,地址为:https://github.com/cglib/cglib为什么

    2022年5月1日
    50
  • Redis介绍——Linux环境Redis安装全过程和遇到的问题及解决方案

    Redis介绍——Linux环境Redis安装全过程和遇到的问题及解决方案

    2022年2月26日
    36
  • vue双向绑定经典案例「建议收藏」

    vue双向绑定经典案例「建议收藏」1、无需废话,直接上代码复制到新建的记事本文件,保存问demo.html即可。<scriptsrc=”https://cdn.staticfile.org/vue/2.2.2/vue.min.js”></script><!DOCTYPEhtml><html><head><metacharset=”utf-8″><title>欢迎系统</title></head>

    2022年9月14日
    5
  • Matlab插值方法大全

    Matlab插值方法大全命令1 interp1功能一维数据插值(表格查找)。该命令对数据点之间计算内插值。它找出一元函数f(x)在中间点的数值。其中函数f(x)由所给数据决定。x:原始数据点Y:原始数据点xi:插值点Yi:插值点格式(1)yi=interp1(x,Y,xi)返回插值向量yi,每一元素对应于参量xi,同时由向量x与Y的内插值决定。参量x指定数据Y的点。若Y为一矩阵,则按Y的

    2022年6月4日
    111
  • vue使用axios解决跨域_vue前端解决跨域的方法

    vue使用axios解决跨域_vue前端解决跨域的方法工具版本:【vue-V】:2.9.6ide工具:VSCode/Idea前提:我们前端vue工程需要单独部署一、本地使用命令运行跨域问题。外网访问的地址:https://www.runoob.com/try/ajax/json_demo.json本地springboot接口访问的地址:http://192.168.3.12:8081/register/getSmsCode/1234567891、axios访问的代码: created(){ const_this=this

    2022年9月11日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号