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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • npm使用淘宝镜像(npm切换淘宝镜像)

    1.通过cnpm使用淘宝镜像:npminstall-gcnpm–registry=https://registry.npm.taobao.org2.将npm设置为淘宝镜像:npmconfigsetregistryhttps://registry.npm.taobao.org3.查看cnpm镜像设置:cnpmconfiggetregistry

    2022年4月10日
    359
  • Json 作为ActionResutl 时出错

    Json 作为ActionResutl 时出错

    2021年8月23日
    66
  • 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
  • Dubbo架构(应用架构)

    一、整体框架1、Dubbo介绍ApacheDubbo是一款高性能、轻量级的开源JavaRPC框架。它有三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。1、Dubbo特点1、面向接口代理的高性能RPC调用:提供高性能的基于代理的远程调用能力,服务以接口为粒度,为开发者屏蔽远程调用底层细节。2、智能负载均衡:内置多种负载均衡策略,智能感知下游节点健康状…

    2022年4月11日
    98
  • Xray的快速使用

    Xray的快速使用快速使用使用基础爬虫爬取并扫描整个网站xraywebscan–basic-crawlerhttp://example.com–html-outputcrawler.html使用HTTP代理进行被动扫描xraywebscan–listen127.0.0.1:7777–html-outputproxy.html设置浏览器http代理为http://127.0.0.1:7777,就可以自动分析代理流量并扫描。如需扫描https流量,请阅读下方文档抓取htt

    2022年5月30日
    67
  • 什么是UDP攻击_机器人打电话营销效果

    什么是UDP攻击_机器人打电话营销效果UDP协议UDP是一个简单的面向数据报的运输层协议,也是最常见的作为流量攻击最多的一种协议,需要用到UDP的主要都是视频通讯,枪战类实时通讯的游戏类。UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但并不保证它们能到达目的地。由于UDP传输数据前传输数据之前源端和终端不建立连接,且没有超时重发等机制,故而传输速度很快。UDP攻击UDP攻击是DDoS攻击的一种,是典型的流量型攻击。就好比学校中午放学时的食堂,学生大量飞奔食堂,但食堂窗口就那么多,学生数量太多,就只能挤在窗口前等待。

    2022年10月2日
    4

发表回复

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

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