Laravel 4 自定义分页:转到第一页/最后一页并显示固定数量的链接 [英] Laravel 4 custom pagination: go to first/last page and show a fixed number of links

查看:12
本文介绍了Laravel 4 自定义分页:转到第一页/最后一页并显示固定数量的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Laravel 4 中使用默认的 Bootstrap 滑块导航,效果很好:但现在我想通过两个自定义来改进它,但我不知道如何:

I'm using the default Bootstrap slider navigation in Laravel 4 and it works very nice: but now I'd like improve it with two customizations, and I have no clue how:

1:在开头添加跳转到第一页",如果在第一页则禁用;最后还有一个类似的转到最后一页",如果我们在最后一页,则禁用(请查看与默认的 Laravel 内容相比,箭头是如何变化的!我想使用双箭头"(« 和 ») 用于转到第一个/最后一个",一个箭头(‹ 和 ›)用于转到上一个/下一个:

1: Add a "go to first page" at the beginning, disabled if we are in the first page; and an analogous "go to last page" at the end, disabled if we are in the last page (please see how the arrows are changed compared to default Laravel stuff! I'd like to use "double arrows" (« and ») for "go to first/last", and a single arrow (‹ and ›) for "go to prev/next:

<ul class="pagination">
    <li class="disabled"><span>&laquo;</span></li>
    <li class="disabled"><span>&lsaquo;</span></li>
    <li class="active"><span>1</span></li>
    <li><a href="http://laravel/games?page=2">2</a></li>
    <li><a href="http://laravel/games?page=3">3</a></li>
    <li><a href="http://laravel/games?page=4">4</a></li>
    <li><a href="http://laravel/games?page=5">5</a></li>
    <li><a href="http://laravel/games?page=6">6</a></li>
    <li><a href="http://laravel/games?page=7">7</a></li>
    <li><a href="http://laravel/games?page=8">8</a></li>
    <li><a href="http://laravel/games?page=9">9</a></li>
    <li><a href="http://laravel/games?page=10">10</a></li>
    <li><a href="http://laravel/games?page=2" rel="next">&rsaquo;</a></li>
    <li><a href="http://laravel/games?page=10" rel="last">&raquo;</a></li>
</ul>

2:一次显示自定义数量的链接(即 5 个)而不是默认的长列表:

2: Show a custom number of links at a time (i.e. 5) instead of the default long list:

<ul class="pagination">
    <li class="disabled"><span>&laquo;</span></li>
    <li class="disabled"><span>&lsaquo;</span></li>
    <li class="active"><span>1</span></li>
    <li><a href="http://laravel/games?page=2">2</a></li>
    <li><a href="http://laravel/games?page=3">3</a></li>
    <li><a href="http://laravel/games?page=4">4</a></li>
    <li><a href="http://laravel/games?page=5">5</a></li>
    <li><a href="http://laravel/games?page=2" rel="next">&rsaquo;</a></li>
    <li><a href="http://laravel/games?page=10" rel="last">&raquo;</a></li>
</ul>

<ul class="pagination">
    <li><a href="http://laravel/games?page=1" rel="first">&laquo;</a></li>
    <li><a href="http://laravel/games?page=3" rel="prev">&lsaquo;</a></li>
    <li><a href="http://laravel/games?page=2">2</a></li>
    <li><a href="http://laravel/games?page=3">3</a></li>
    <li class="active"><span>4</span></li>
    <li><a href="http://laravel/games?page=5">5</a></li>
    <li><a href="http://laravel/games?page=6">6</a></li>
    <li><a href="http://laravel/games?page=4" rel="next">&rsaquo;</a></li>
    <li><a href="http://laravel/games?page=10" rel="last">&raquo;</a></li>
</ul>

想看图吗?这就是各种页面的样子(它是一个建立在 Bootstrap 2.3.2 上的旧程序 PHP ......我有原始代码但不知道如何翻译"成 Laravel OOP)

Want to see a picture? This is how it should be for various pages (it's an old procedural PHP built on Bootstrap 2.3.2... I have the original code but don't know how to "translate" into Laravel OOP)

到目前为止,我创建了一个 custom.php 视图并更改了行 'pagination' =>'pagination::slider-3' 在 laravel/app/config/view.php 到 'pagination' =>'分页::自定义'

So far I made a custom.php view and changed line 'pagination' => 'pagination::slider-3' in laravel/app/config/view.php to 'pagination' => 'pagination::custom'

然后,在 custom.php 视图中,我被困在:

Then, in custom.php view, I am stuck on:

<?php
    $presenter = new IlluminatePaginationBootstrapPresenter($paginator);
?>

<?php if ($paginator->getLastPage() > 1): ?>
    <ul class="pagination">

    </ul>
<?php endif; ?>

请问有什么帮助吗?

提前致谢!

推荐答案

给你.

<?php 

class MyPresenter extends IlluminatePaginationBootstrapPresenter {

    public function render()
    {
        if ($this->currentPage == 1) {
            $content = $this->getPageRange(1, $this->currentPage + 2); 
        }
        else if ($this->currentPage >= $this->lastPage) {
            $content = $this->getPageRange($this->currentPage - 2, $this->lastPage); 
        }
        else {
            $content = $this->getPageRange($this->currentPage - 1, $this->currentPage + 1); 
        }

        return $this->getFirst().$this->getPrevious('&lsaquo;').$content.$this->getNext('&rsaquo;').$this->getLast();
    }

    public function getFirst($text = '&laquo;')
    {
        if ($this->currentPage <= 1)
        {
            return $this->getDisabledTextWrapper($text);
        }
        else
        {
            $url = $this->paginator->getUrl(1);
            return $this->getPageLinkWrapper($url, $text);
        }
    }

    public function getLast($text = '&raquo;')
    {
        if ($this->currentPage >= $this->lastPage)
        {
            return $this->getDisabledTextWrapper($text);
        }
        else
        {
            $url = $this->paginator->getUrl($this->lastPage);

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

随意更改 MyPresenter 类名,并在您的自定义视图中:

Feel free to change MyPresenter class name, and in your custom view:

<ul class="pagination">
    <?php echo with(new MyPresenter($paginator))->render(); ?>
</ul>

当然这并不完美(如果总页数少于 3,您可能会遇到问题),但这会让您走上正轨.

Of course this is not quite perfect (you'll probably run into problem if total pages is less than 3), but this will set you on the right track.

这篇关于Laravel 4 自定义分页:转到第一页/最后一页并显示固定数量的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆