Limit amount of links shown with Laravel pagination

前端 未结 11 2222
一个人的身影
一个人的身影 2020-12-14 02:10

Any easy way to limit how many links are shown with Laravels pagination?

Currently it shows 13 links at most (Prev, 1 2 3 4 5 7 8 .. 78 79 next)

This however

11条回答
  •  难免孤独
    2020-12-14 02:16

    I had created a new custom presenter to show only 10 links. It involves 3 steps:

    Create your own custom presenter

    use Illuminate\Pagination\BootstrapPresenter;
    
    class CustomPresenter extends BootstrapPresenter{
        protected function getPageSlider()
        {
            // Changing the original value from 6 to 3 to reduce the link count
            $window = 3;
    
            // If the current page is very close to the beginning of the page range, we will
            // just render the beginning of the page range, followed by the last 2 of the
            // links in this list, since we will not have room to create a full slider.
            if ($this->currentPage <= $window)
            {
                $ending = $this->getFinish();
    
                return $this->getPageRange(1, $window + 2).$ending;
            }
    
            // If the current page is close to the ending of the page range we will just get
            // this first couple pages, followed by a larger window of these ending pages
            // since we're too close to the end of the list to create a full on slider.
            elseif ($this->currentPage >= $this->lastPage - $window)
            {
                $start = $this->lastPage - 8;
    
                $content = $this->getPageRange($start, $this->lastPage);
    
                return $this->getStart().$content;
            }
    
            // If we have enough room on both sides of the current page to build a slider we
            // will surround it with both the beginning and ending caps, with this window
            // of pages in the middle providing a Google style sliding paginator setup.
            else
            {
                $content = $this->getAdjacentRange();
    
                return $this->getStart().$content.$this->getFinish();
            }
        }
    
    } 
    

    Create your own pagination view (e.g. custom-paginator.php), place in your views folder

      render(); ?>

    Update your app/config.view.php

    'pagination' => 'custom-paginator',
    

    By making the following changes, you will able to get a 10 links paginator.

    Hope this help :D

提交回复
热议问题