本文實例講述了Laravel框架分頁實現方法。分享給大家供大家參考,具體如下:
Laravel使用的過程中,有些功能把前端頁面的表達“寫死了”,比如分頁的翻頁按鈕!
當然你會說Laravel的Bootstrap樣式也很好看啊,但是實際項目中,翻頁按鈕常常需要滿足的客戶的需要,特別在開發一款支持手機適配的Web APP,更是需要使用自定義的樣式。
所以,學習一樣東西不能一知半解,而是究其原理。
先來看看Laravel是怎么分頁的,生成分頁按鈕的代碼究竟寫在了哪里?
Laravel目錄\vendor\laravel\framework\src\Illuminate\Pagination下
先理一下類的繼承關系
PresenterContract(父類)
┗BootstrapThreePresenter(子類)<-SimpleBootstrapThreePresenter
┗BootstrapFourPresenter(子類)<-SimpleBootstrapFourPresenter
從作者對類的命名上看,必有區別,我們從代碼上研究
BootstrapThreePresenter.php和BootstrapFourPresenter.php主要區別在下列函數
BootstrapThreePresenter.php代碼:
/** * Get HTML wrapper for an available page link. * * @param string $url * @param int $page * @param string|null $rel * @return string */ protected function getAvailablePageWrapper($url, $page, $rel = null) { $rel = is_null($rel) ? '' : ' rel="'.$rel.'"'; return '<li><a href="'.htmlentities($url).'" rel="external nofollow" rel="external nofollow" '.$rel.'>'.$page.'</a></li>'; } /** * Get HTML wrapper for disabled text. * * @param string $text * @return string */ protected function getDisabledTextWrapper($text) { return '<li class="disabled"><span>'.$text.'</span></li>'; } /** * Get HTML wrapper for active text. * * @param string $text * @return string */ protected function getActivePageWrapper($text) { return '<li class="active"><span>'.$text.'</span></li>'; }
BootstrapFourPresenter.php代碼:
/** * Get HTML wrapper for an available page link. * * @param string $url * @param int $page * @param string|null $rel * @return string */ protected function getAvailablePageWrapper($url, $page, $rel = null) { $rel = is_null($rel) ? '' : ' rel="'.$rel.'"'; return '<li class="page-item"><a class="page-link" href="'.htmlentities($url).'" rel="external nofollow" rel="external nofollow" '.$rel.'>'.$page.'</a></li>'; } /** * Get HTML wrapper for disabled text. * * @param string $text * @return string */ protected function getDisabledTextWrapper($text) { return '<li class="page-item disabled"><a class="page-link">'.$text.'</a></li>'; } /** * Get HTML wrapper for active text. * * @param string $text * @return string */ protected function getActivePageWrapper($text) { return '<li class="page-item active"><a class="page-link">'.$text.'</a></li>'; }
我們發現最大的區別在ThreePresenter幾乎是“裸”HTML標簽,而FourPresenter生成的是帶class的HTML標簽。
無論是ThreePresenter還是FourPresenter,他們都有一個相同實現的render()函數
/** * Convert the URL window into Bootstrap HTML. * * @return \Illuminate\Support\HtmlString */ public function render() { if ($this->hasPages()) { return new HtmlString(sprintf( '<ul class="pagination">%s %s %s</ul>', $this->getPreviousButton(), $this->getLinks(), $this->getNextButton() )); } return ''; }
細心的讀者已經發覺,還有兩個繼承類,分別是SimpleThreePresenter和SimpleFourPresenter,既然是Simple(簡單),區別就在他們的render()函數
/** * Convert the URL window into Bootstrap HTML. * * @return \Illuminate\Support\HtmlString */ public function render() { if ($this->hasPages()) { return new HtmlString(sprintf( '<ul class="pager">%s %s</ul>', $this->getPreviousButton(), $this->getNextButton() )); } return ''; }
也就是說,SimpleThreePresenter和SimpleFourPresenter生成的分頁按鈕是沒有“頁碼”的,只有“上一頁”和“下一頁”按鈕。
希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。