Last active
February 24, 2023 18:41
-
-
Save jamisonbryant/41a4540644101c6eb041 to your computer and use it in GitHub Desktop.
Laravel paginator for Materialize framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Custom\Pagination\Presenters; | |
| use Illuminate\Pagination\BootstrapThreePresenter; | |
| use Illuminate\Pagination\LengthAwarePaginator; | |
| use Illuminate\Pagination\UrlWindow; | |
| class MaterializePresenter extends BootstrapThreePresenter | |
| { | |
| /** | |
| * @var int Number of links to display on each end of paginator | |
| */ | |
| const LINK_BUFFER_SIZE = 3; | |
| /** | |
| * Create a new presenter instance | |
| */ | |
| public function __construct(LengthAwarePaginator $paginator) | |
| { | |
| $this->paginator = $paginator; | |
| $this->window = UrlWindow::make($paginator, self::LINK_BUFFER_SIZE); | |
| } | |
| /** | |
| * Render paginator | |
| * | |
| * @return string | |
| */ | |
| public function render() | |
| { | |
| if ($this->hasPages()) { | |
| return sprintf('<ul class="pagination">%s %s %s</ul>', | |
| $this->getPrevious(), $this->getLinks(), $this->getNext()); | |
| } else { | |
| return ''; | |
| } | |
| } | |
| /** | |
| * Get HTML wrapper for page link | |
| * | |
| * @param string $url Link URL | |
| * @param string $text Link text | |
| * @param string $rel Link relationship (not used) | |
| * @return string | |
| */ | |
| public function getPageLinkWrapper($url, $text, $rel = null) | |
| { | |
| if ($text == $this->paginator->currentPage()) { | |
| return '<li class="active"><a href="' . $url . '">' . $text . '</a></li>'; | |
| } else { | |
| return '<li class="waves-effect"><a href="' . $url . '">' . $text . '</a></li>'; | |
| } | |
| } | |
| /** | |
| * Get HTML wrapper for disabled text | |
| * | |
| * @param string $text | |
| * @return string | |
| */ | |
| public function getDisabledTextWrapper($text) | |
| { | |
| return '<li class="disabled"><a href="javascript:void(0)">' . $text . '</a></li>'; | |
| } | |
| /** | |
| * Get previous page pagination element | |
| * | |
| * @return string | |
| */ | |
| public function getPrevious() | |
| { | |
| if ($this->paginator->currentPage() <= 1) { | |
| return $this->getDisabledTextWrapper('<i class="mdi-navigation-chevron-left"></i>'); | |
| } else { | |
| $url = $this->paginator->url($this->paginator->currentPage() - 1); | |
| return $this->getPageLinkWrapper($url, '<i class="mdi-navigation-chevron-left"></i>'); | |
| } | |
| } | |
| /** | |
| * Get next page pagination element | |
| * | |
| * @return string | |
| */ | |
| public function getNext() | |
| { | |
| if ($this->paginator->currentPage() >= $this->paginator->lastPage()) { | |
| return $this->getDisabledTextWrapper('<i class="mdi-navigation-chevron-right"></i>'); | |
| } else { | |
| $url = $this->paginator->url($this->paginator->currentPage() + 1); | |
| return $this->getPageLinkWrapper($url, '<i class="mdi-navigation-chevron-right"></i>'); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment