普通数组分页,自定义分页驱动器类【记录一下】

浏览:50 发布日期:2020/03/26 分类:ThinkPHP5专区 关键字: 分页|thinkphp5.1
使用 ThinkPHP5.1 分页 , 其分页驱动Bootstrap类【thinkphp\library\think\paginator\dirver\Bootstrap.php】
由于缺乏css样式,并且有时候,根本就不想引入bootstrap的分页样式,在不改动框架的基础上,我在common模型里面添加了util工具类,小小改动一下,增加一些css样式在里面,保证每一次调用都不用重新引入分页的样式。
由于我使用了layui前端框架所以,所以就再增加一个layui的分页驱动。
今天突然需要常规【二维】数组分页【不是mysql数据】,于是又重新改了分页驱动以便满足于常规数组的分页,常规【二维】数组分页,同时模仿了where,whereOr,withAttr这三个方法【因为我常用这几个方法】常规【二维】数组分页 测试例子
 for ($i = 0; $i < 100; ++$i) {
            $arr[] = ['id' => $i, 'name' => '李' . rand(0, 100), 'age' => rand(0, 200)];
        }

        $page = new Bootstrap($arr, 10);
        $page->setCustomArray(true);
        $page->withAttr("name", function ($value, $data) {
            return "姓名:" . $value;
        });
        $page->where([['id', '<', 60]]);

        $page->whereOr([['id', '<', 30], ['id', '>', 50]]);
        $page->withAttr("age", function ($value, $data) {
            return "年龄:" . $value;
        });
        $list = $page->getContent();
        // return json(['content' => $list]);
        $total = $page->getTotal();
        $result = $page->render();

        return json(['result' => true, 'render' => $result, 'total' => $total, 'data' => $list]);
<?php

/**
 *  +----------------------------------------------------------------------
 *  | ThinkPHP [ WE CAN DO IT JUST THINK ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2019 ahai574 All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed ( ++++ahai574++++ )
 *  +----------------------------------------------------------------------
 *  | Author: 阿海 <764882431@qq.com>
 *  +----------------------------------------------------------------------
 */

namespace app\common\util;

use think\Exception;
use think\Paginator as Page;

class Paginator extends Page
{
    /**
     * 临时存放满足条件的数据数组
     */
    private $tempArray = [];

    /**
     * 以tp内置的paginate类的参数为基准
     */
    public function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = [])
    {
        parent::__construct($items, $listRows, $currentPage, $total, $simple, $options);
    }

    /**
     * 并查询
     * @param $param 规定查询条件的格式,条件使用二维数组 比如:[['id','>','15']]
     */
    public function where($param)
    {
        if (!is_array($param) && count($param) !== count($param, COUNT_RECURSIVE)) {
            throw new Exception("参数必须是二维数组");
        }

        $this->tempArray = count($this->tempArray) > 0 ? $this->tempArray : $this->items;

        $list = array();
        foreach ($param as $k => $v) {
            //目前只做“like”的模糊查找 以及一些等式运算
            if ($v[1] != 'like') {
                foreach ($this->tempArray as $kk => $vv) {
                    switch ($v[1]) {
                        case ">":
                        case "gt":
                            $flag = ($vv[$v[0]] > $v[2]);
                            break;
                        case "<":
                        case "lt":
                            $flag = ($vv[$v[0]] < $v[2]);
                            break;
                        case "=":
                        case "eq":
                            $flag = ($vv[$v[0]] == $v[2]);
                            break;
                        case "egt":
                        case ">=":
                            $flag = ($vv[$v[0]] >= $v[2]);
                            break;
                        case "<=":
                        case "elt":
                            $flag = ($vv[$v[0]] <= $v[2]);
                            break;
                        case "<>":
                        case "!=":
                        case "neq":
                            $flag = ($vv[$v[0]] != $v[2]);
                            break;
                        default:
                            throw new Exception("未识别判断条件:" . $v[1]);
                            break;
                    }

                    if ($flag) {
                        //处理获取器
                        if (isset($this->options['with_attr']) && is_array($this->options['with_attr'])) {
                            foreach ($this->options['with_attr'] as $key => $val) {
                                //模拟tp的withAttr                  
                                $vv[$key] = call_user_func_array($val, array($vv[$key], $vv));
                            }
                        }
                        $list[] = $vv;
                    }
                }
            } else {
                //模糊查询
                foreach ($this->tempArray as $kk => $vv) {
                    if (strpos($vv[$v[0]], $v[2]) !== false) {
                        //处理获取器
                        if (isset($this->options['with_attr']) && is_array($this->options['with_attr'])) {
                            foreach ($this->options['with_attr'] as $key => $val) {
                                //模拟tp的withAttr
                                $vv[$key] = call_user_func_array($val, array($vv[$key], $vv));
                            }
                        }
                        $list[] = $vv;
                    }
                }
            }

            //遍历结束后,必须在此处将获取器清空,避免再次被使用
            $this->options['with_attr'] = [];
            //并查询 放在foreach 保证下一个查询条件
            // $this->items = $list;
            $this->tempArray = $list;
            //重置数组
            $list = [];
        }

        // $this->total = count($this->items);
        $this->total = count($this->tempArray);
        //取出对应页码的数据
        // $output = array_slice($this->items, (($this->currentPage) - 1) * ($this->listRows), $this->listRows);
        $output = array_slice($this->tempArray, (($this->currentPage) - 1) * ($this->listRows), $this->listRows);

        return $this->items = $output;
    }

    /**
     * 或查询
     * @param $param 规定查询条件的格式,条件使用二维数组 比如:[['id','>','15']]
     */
    public function whereOr($param)
    {
        if (!is_array($param) && count($param) !== count($param, COUNT_RECURSIVE)) {
            throw new Exception("参数必须是二维数组");
        }
        $list = array();
        $this->tempArray = count($this->tempArray) > 0 ? $this->tempArray : $this->items;

        foreach ($param as $k => $v) {
            //目前只做“like”的模糊查找 以及一些等式运算
            if ($v[1] != 'like') {
                foreach ($this->tempArray as $kk => $vv) {
                    switch ($v[1]) {
                        case ">":
                        case "gt":
                            $flag = ($vv[$v[0]] > $v[2]);
                            break;
                        case "<":
                        case "lt":
                            $flag = ($vv[$v[0]] < $v[2]);
                            break;
                        case "=":
                        case "eq":
                            $flag = ($vv[$v[0]] == $v[2]);
                            break;
                        case "egt":
                        case ">=":
                            $flag = ($vv[$v[0]] >= $v[2]);
                            break;
                        case "<=":
                        case "elt":
                            $flag = ($vv[$v[0]] <= $v[2]);
                            break;
                        case "<>":
                        case "!=":
                        case "neq":
                            $flag = ($vv[$v[0]] != $v[2]);
                            break;
                        default:
                            throw new Exception("未识别判断条件:" . $v[1]);
                            break;
                    }

                    if ($flag) {
                        //处理获取器
                        if (isset($this->options['with_attr']) && is_array($this->options['with_attr'])) {
                            foreach ($this->options['with_attr'] as $key => $val) {
                                //模拟tp的withAttr
                                $vv[$key] = call_user_func_array($val, array($vv[$key], $vv));
                            }
                        }
                        $list[] = $vv;
                    }
                }
            } else {
                //模糊查询
                foreach ($this->tempArray as $kk => $vv) {
                    if (strpos($vv[$v[0]], $v[2]) !== false) {
                        //处理获取器
                        if (isset($this->options['with_attr']) && is_array($this->options['with_attr'])) {
                            foreach ($this->options['with_attr'] as $key => $val) {
                                //模拟tp的withAttr
                                $vv[$key] = call_user_func_array($val, array($vv[$key], $vv));
                            }
                        }
                        $list[] = $vv;
                    }
                }
            }

            //遍历结束后,必须在此处将获取器清空,避免再次被使用
            $this->options['with_attr'] = [];
        }

        //或查询
        $this->tempArray = $list;

        $this->total = count($this->tempArray);

        //取出对应页码的数据
        $output = array_slice($this->tempArray, (($this->currentPage) - 1) * ($this->listRows), $this->listRows);

        return $this->items = $output;
    }

    /**
     * 获取器 --- 将结果集进行处理输出
     */
    public function withAttr($name, $callback = null)
    {
        if (is_array($name)) {
            $this->options['with_attr'] = $name;
        } else {
            $this->options['with_attr'][$name] = $callback;
        }

        return $this;
    }

    /**
     * 数据集
     * @return array
     */
    public function getContent()
    {
        
        if (isset($this->options['with_attr']) && is_array($this->options['with_attr'])) {
            //处理获取器
            $list =  array();
        
            foreach ($this->items as $kk => $vv) {
                foreach ($this->options['with_attr'] as $key => $val) {
                    //模拟tp的withAttr
                    $vv[$key] = call_user_func_array($val, array($vv[$key], $vv));
                }
                $list[] = $vv;
            }
            $this->items = $list;
        }

        return $this->items;
    }

    /**
     * 总数
     * @return int
     */
    public function getTotal()
    {
        return $this->total;
    }

    /**
     * 渲染分页html
     * @access public
     * @return mixed
     */
    public function render()
    {
        // TODO: Implement render() method.
    }

    /**
     * bootstrap 分页样式
     */
    protected function getBootstrapStyle()
    {
        return "<style>.pagination{width:100%;display:inline-block;padding-left:0;margin:20px 0;border-radius:4px}.pagination>li{display:inline}.pagination>li:first-child>span{margin-left:0;border-top-left-radius:4px;border-bottom-left-radius:4px}.pagination>.disabled>span,.pagination>.disabled>span:hover,.pagination>.disabled>span:focus,.pagination>.disabled>a,.pagination>.disabled>a:hover,.pagination>.disabled>a:focus{color:#777;cursor:not-allowed;background-color:#fff;border-color:#ddd}.pagination>.active>a,.pagination>.active>span,.pagination>.active>a:hover,.pagination>.active>span:hover,.pagination>.active>a:focus,.pagination>.active>span:focus{z-index:3;color:#fff;cursor:default;background-color:#337ab7;border-color:#337ab7}.pagination>li>a,.pagination>li>span{position:relative;float:left;padding:6px 12px;margin-left:-1px;line-height:1.42857143;color:#337ab7;text-decoration:none;background-color:#fff;border:1px solid #ddd}.pagination>li>a,.pagination>li>span{position:relative;float:left;padding:6px 12px;margin-left:-1px;line-height:1.42857143;color:#337ab7;text-decoration:none;background-color:#fff;border:1px solid #ddd}li{list-style:none}</style>";
    }

    /**
     * layui 分页样式 --ajax 分页使用
     */
    protected function getLayuiStyle()
    {
        return "<style>.layui-laypage{width:100%;display:inline-block;*display:inline;*zoom:1;vertical-align:middle;margin:10px 0;font-size:0}.layui-box,.layui-box *{box-sizing:content-box}.layui-laypage>a:first-child,.layui-laypage>a:first-child em{border-radius:2px 0 0 2px}.layui-laypage>:first-child{margin-left:0!important}.layui-laypage a{text-decoration:none}.layui-laypage a,.layui-laypage span{display:inline-block;*display:inline;*zoom:1;vertical-align:middle;padding:0 15px;height:28px;line-height:28px;margin:0 -1px 5px 0;background-color:#fff;color:#333;font-size:12px}.layui-laypage a,.layui-laypage button,.layui-laypage input,.layui-laypage select,.layui-laypage span{border:1px solid #e2e2e2}.layui-disabled,.layui-disabled:hover{color:#d2d2d2!important;cursor:not-allowed!important}.layui-btn,.layui-disabled,.layui-icon,.layui-unselect{-moz-user-select:none;-webkit-user-select:none;-ms-user-select:none}.layui-laypage .layui-laypage-curr{position:relative}.layui-laypage a,.layui-laypage span{display:inline-block;*display:inline;*zoom:1;vertical-align:middle;padding:0 15px;height:28px;line-height:28px;margin:0 -1px 5px 0;background-color:#fff;color:#333;font-size:12px}.layui-laypage a,.layui-laypage button,.layui-laypage input,.layui-laypage select,.layui-laypage span{border:1px solid #e2e2e2}.layui-laypage .layui-laypage-curr .layui-laypage-em{position:absolute;left:-1px;top:-1px;padding:1px;width:100%;height:100%;background-color:#1E9FFF}.layui-laypage .layui-laypage-curr em{position:relative;color:#fff}.layui-laypage em{font-style:normal}.layui-laypage-em{border-radius:2px}</style>";
    }
}
<?php

/**
 *  +----------------------------------------------------------------------
 *  | ThinkPHP [ WE CAN DO IT JUST THINK ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2019 ahai574 All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed ( ++++ahai574++++ )
 *  +----------------------------------------------------------------------
 *  | Author: 阿海 <764882431@qq.com>
 *  +----------------------------------------------------------------------
 */

namespace app\common\util\page\driver;

use app\common\util\Paginator;
use think\Exception;

class Bootstrap extends Paginator
{
    /**
     * 是否简洁模式
     * @var bool
     */
    protected $simple = false;

    /**
     * 数据集
     * @var Collection
     */
    protected $items;

    /**
     * 当前页
     * @var integer
     */
    protected $currentPage;

    /**
     * 最后一页
     * @var integer
     */
    protected $lastPage;

    /**
     * 数据总数
     * @var integer|null
     */
    protected $total;

    /**
     * 每页数量
     * @var integer
     */
    protected $listRows;

    /**
     * 是否有下一页
     * @var bool
     */
    protected $hasMore;

    /**
     * 分页配置
     * @var array
     */
    protected $options = [
        'var_page' => 'page',
        'path'     => '/',
        'query'    => [],
        'fragment' => '',
    ];

    /**
     * 区分是数据库 还是 数组数据  默认都是数据库数据
     */
    private $isArray = false;

    public function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = [], $isArray = false)
    {
        if (!isset($options['path']) || $options['path'] == "/") {
            $options = [
                'path' => request()->path() . "." . config("app.url_html_suffix"),
                // 'query' => ['id' => 1, 'u' => 2] //url后面的携带参数---可以在url上面比较直观的看见参数,
            ];
        }
        //如果是数组,那么就是自定义查询,不走tp的数据库查询
        if (!$this->isArray) {
            parent::__construct($items, $listRows, $currentPage, $total, $simple, $options);
            $this->items = $this->getContent();
            $this->listRows = (isset($listRows) && (int) $listRows > 0) ? (int) $listRows : 15;
            $this->currentPage = ((int) input($this->options['var_page']) > 1) ? (int) input($this->options['var_page']) : (((int) $currentPage < 1) ? 1 : (int) $currentPage);
            $this->total = count($this->getContent());
            $this->simple = $simple;
            $this->options = array_merge($this->options, $options);
        }
    }

    /**
     * 设置自定义查询数组
     */
    public function setCustomArray($isArray = true)
    {
        return $this->isArray = $isArray;
    }

    /**
     * 实例化
     */
    public function store()
    {
        if (!is_array($this->items) && count($this->items) !== count($this->items, COUNT_RECURSIVE)) {
            throw new Exception("数据结构不正确,必须是二维数组");
        }
        parent::__construct($this->items, $this->listRows, $this->currentPage, $this->total, $this->simple, $this->options);
    }

    /**
     * 上一页按钮
     * @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->isArray) {
            $this->store();
        }
        if ($this->hasPages()) {
            if ($this->simple) {
                return sprintf(
                    '%s <ul class="pager pagination">%s %s</ul>',
                    $this->getBootstrapStyle(),
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    '%s <ul class="pagination layui-col-xs12 layui-col-sm12 layui-col-md12 layui-col-lg12 col-xs-12 col-sm-12 col-md-12 col-lg-12">%s %s %s</ul>',
                    $this->getBootstrapStyle(),
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton()
                );
            }
        }
    }

    /**
     * 生成一个可点击的按钮
     *
     * @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 ($this->currentPage() == $page) {
            return $this->getActivePageWrapper($page);
        }

        return $this->getAvailablePageWrapper($url, $page);
    }
}
<?php

/**
 *  +----------------------------------------------------------------------
 *  | ThinkPHP [ WE CAN DO IT JUST THINK ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2019 ahai574 All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed ( ++++ahai574++++ )
 *  +----------------------------------------------------------------------
 *  | Author: 阿海 <764882431@qq.com>
 *  +----------------------------------------------------------------------
 */

namespace app\common\util\page\driver;

use app\common\util\Paginator;

class Layui extends Paginator
{
     /**
     * 是否简洁模式
     * @var bool
     */
    protected $simple = false;

    /**
     * 数据集
     * @var Collection
     */
    protected $items;

    /**
     * 当前页
     * @var integer
     */
    protected $currentPage;

    /**
     * 最后一页
     * @var integer
     */
    protected $lastPage;

    /**
     * 数据总数
     * @var integer|null
     */
    protected $total;

    /**
     * 每页数量
     * @var integer
     */
    protected $listRows;

    /**
     * 是否有下一页
     * @var bool
     */
    protected $hasMore;

    /**
     * 分页配置
     * @var array
     */
    protected $options = [
        'var_page' => 'page',
        'path'     => '/',
        'query'    => [],
        'fragment' => '',
    ];

    /**
     * 区分是数据库 还是 数组数据  默认都是数据库数据
     */
    private $isArray = false;

    public function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = [], $isArray = false)
    {
        if (!isset($options['path']) || $options['path'] == "/") {
            $options = [
                'path' => request()->path() . "." . config("app.url_html_suffix"),
                // 'query' => ['id' => 1, 'u' => 2] //url后面的携带参数---可以在url上面比较直观的看见参数,
            ];
        }
        //如果是数组,那么就是自定义查询,不走tp的数据库查询
        if (!$this->isArray) {
            parent::__construct($items, $listRows, $currentPage, $total, $simple, $options);
            $this->items = $this->getContent();
            $this->listRows = (isset($listRows) && (int) $listRows > 0) ? (int) $listRows : 15;
            $this->currentPage = ((int) input($this->options['var_page']) > 1) ? (int) input($this->options['var_page']) : (((int) $currentPage < 1) ? 1 : (int) $currentPage);
            $this->total = count($this->getContent());
            $this->simple = $simple;
            $this->options = array_merge($this->options, $options);
        }
    }

    /**
     * 设置自定义查询数组
     */
    public function setCustomArray($isArray = true)
    {
        return $this->isArray = $isArray;
    }

    /**
     * 实例化
     */
    public function store()
    {
        if (!is_array($this->items) && count($this->items) !== count($this->items, COUNT_RECURSIVE)) {
            throw new Exception("数据结构不正确,必须是二维数组");
        }  
        parent::__construct($this->items, $this->listRows, $this->currentPage, $this->total, $this->simple, $this->options);
    }

    /**
     * 上一页按钮
     * @param string $text "上一页"|"«"
     * @return string
     */
    protected function getPreviousButton($text = "上一页")
    {

        if ($this->currentPage() <= 1) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url(
            $this->currentPage() - 1
        );

        $ajaxUrl = $this->currentPage() - 1;

        return $this->getPageLinkWrapper($url, $text, $ajaxUrl);
    }

    /**
     * 按钮
     * @param string $text '»'|下一页
     * @return string
     */
    protected function getNextButton($text = '下一页')
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url($this->currentPage() + 1);

        $ajaxUrl = $this->currentPage() + 1;

        return $this->getPageLinkWrapper($url, $text, $ajaxUrl);
    }

    /**
     * 页码按钮
     * @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->isArray) {
            $this->store();
        }
        if ($this->hasPages()) {
            if ($this->simple) {
                return sprintf(
                    '%s <div class="pager layui-box layui-laypage layui-laypage-default">%s %s</div>',
                    $this->getLayuiStyle(),
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    '%s <div class="layui-box layui-laypage layui-laypage-default layui-col-xs12 layui-col-sm12 layui-col-md12 layui-col-lg12 col-xs-12 col-sm-12 col-md-12 col-lg-12">%s %s %s</div>',
                    $this->getLayuiStyle(),
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton()
                );
            }
        }
    }

    /**
     * 生成一个可点击的按钮
     *
     * @param  string $url
     * @param  int    $page
     * @return string
     */
    protected function getAvailablePageWrapper($url, $page, $ajaxUrl = '')
    {

        return '<a href="javascript:;" data-page="' . htmlentities($ajaxUrl) . '" data-query="' . htmlentities($url) . '">' . $page . '</a>';
    }

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

    /**
     * 生成一个激活的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getActivePageWrapper($text)
    {
        return '<span class="active 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, $ajaxUrl = '')
    {
        if ($this->currentPage() == $page) {
            return $this->getActivePageWrapper($page);
        }

        return $this->getAvailablePageWrapper($url, $page, $ajaxUrl);
    }
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>