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

查看:80
本文介绍了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东西相比,箭头是如何变化的!我想使用双箭头"(«和» ;)表示转到第一个/最后一个",单箭头(&lsaquo和›)表示转到上一个/下一个:

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视图,并将laravel/app/config/view.php中的行'pagination' => 'pagination::slider-3'更改为'pagination' => 'pagination::custom'

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 Illuminate\Pagination\BootstrapPresenter($paginator);
?>

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

    </ul>
<?php endif; ?>

请帮忙吗?

提前谢谢!

推荐答案

在这里.

<?php 

class MyPresenter extends Illuminate\Pagination\BootstrapPresenter {

    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天全站免登陆