关于分页方法的实现细节

freegun100 2018-04-17 04:00:53
数组 $a = array(1,2,3,4,5,6,7); 1-n的自然数数组,长度随机

现在对$a分页 需要传递 3个参数 总数量,每页显示数量,分页链接最多页码(不然页码链接会撑爆)

现在关键是第三个参数,如何判定在哪一页,是否显示上下页,

这个思路是怎样的,感觉判断的边界非常复杂,

循环如何与第三个参数关联?

有没有大神详细说下解决这个问题的思路和经过.
...全文
814 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
伟洪winni 2018-04-18
  • 打赏
  • 举报
回复

<?php

namespace tool;

use think\Paginator;

class TpPageHelper extends Paginator
{
    //TODO : 中间按钮个数
    protected $bnumber = 10;

    //TODO : 首页
    protected function getfirstButton($str = '')
    {
        if ($this->currentPage() <= 1) {
            return $this->getDisabledTextWrapper($str);
        }

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

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

    //TODO : 上一页
    protected function getPreviousButton($text = "«")
    {

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

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

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

    //TODO : 页码
    protected function getLinks()
    {
        if ($this->total > $this->listRows) {
            if ($this->lastPage < $this->bnumber) {
                return $this->getUrlLinks($this->getUrlRange(1, $this->lastPage));
            } else {
                $min = 1;
                if ($this->currentPage > $this->bnumber / 2) $min = $this->currentPage - floor($this->bnumber / 2);
                if ($this->lastPage - $this->currentPage < $this->bnumber / 2) $min = $this->lastPage - $this->bnumber + 1;
                return $this->getUrlLinks($this->getUrlRange($min, $min + $this->bnumber - 1));
            }
        }
    }

    //TODO : 下一页
    protected function getNextButton($text = '»')
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

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

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

    //TODO : 末页
    protected function getlastButton($text = '')
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url($this->lastPage());

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

    //TODO : 渲染页
    public function render()
    {
        //数据是否足够分页
        if ($this->hasPages()) {
            return sprintf(
                '<ul class="btn-item fr">%s %s %s %s %s</ul>',
                $this->getfirstButton('首页'),
                $this->getPreviousButton('上一页'),
                $this->getLinks(),
                $this->getNextButton('下一页'),
                $this->getlastButton('末页')
            );
        }
    }

    //TODO : 生成禁用按钮
    protected function getDisabledTextWrapper($text)
    {
        return '<li class="disabled"><span>' . $text . '</span></li>';
    }

    //TODO : 生成普通按钮
    protected function getPageLinkWrapper($url, $page)
    {
        if ($page == $this->currentPage()) {
            return $this->getActivePageWrapper($page);
        }

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

    //TODO : 生成当前页按钮
    protected function getActivePageWrapper($text)
    {
        return '<li class="active"><span>' . $text . '</span></li>';
    }

    //TODO : 可点击按钮
    protected function getAvailablePageWrapper($url, $page)
    {
        return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
    }

    //TODO : 批量生成页码按钮
    protected function getUrlLinks(array $urls)
    {
        $html = '';

        foreach ($urls as $page => $url) {
            $html .= $this->getPageLinkWrapper($url, $page);
        }

        return $html;
    }

}

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧