Laravel 4 custom pagination: go to first/last page and show a fixed number of links

你离开我真会死。 提交于 2020-01-10 20:07:31

问题


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: 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: 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>

or

<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>

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)

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'

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; ?>

Please, any help?

Thanks in advance!


回答1:


Here you go.

<?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);
        }
    }
}

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

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

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.



来源:https://stackoverflow.com/questions/25567741/laravel-4-custom-pagination-go-to-first-last-page-and-show-a-fixed-number-of-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!