Laravel框架分頁(yè)實(shí)現(xiàn)方法分析
本文實(shí)例講述了Laravel框架分頁(yè)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
Laravel使用的過(guò)程中,有些功能把前端頁(yè)面的表達(dá)“寫死了”,比如分頁(yè)的翻頁(yè)按鈕!
當(dāng)然你會(huì)說(shuō)Laravel的Bootstrap樣式也很好看啊,但是實(shí)際項(xiàng)目中,翻頁(yè)按鈕常常需要滿足的客戶的需要,特別在開(kāi)發(fā)一款支持手機(jī)適配的Web APP,更是需要使用自定義的樣式。
所以,學(xué)習(xí)一樣?xùn)|西不能一知半解,而是究其原理。
先來(lái)看看Laravel是怎么分頁(yè)的,生成分頁(yè)按鈕的代碼究竟寫在了哪里?
Laravel目錄\vendor\laravel\framework\src\Illuminate\Pagination下
先理一下類的繼承關(guān)系
PresenterContract(父類)
┗BootstrapThreePresenter(子類)<-SimpleBootstrapThreePresenter
┗BootstrapFourPresenter(子類)<-SimpleBootstrapFourPresenter
從作者對(duì)類的命名上看,必有區(qū)別,我們從代碼上研究
BootstrapThreePresenter.php和BootstrapFourPresenter.php主要區(qū)別在下列函數(shù)
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>'; }
我們發(fā)現(xiàn)最大的區(qū)別在ThreePresenter幾乎是“裸”HTML標(biāo)簽,而FourPresenter生成的是帶class的HTML標(biāo)簽。
無(wú)論是ThreePresenter還是FourPresenter,他們都有一個(gè)相同實(shí)現(xiàn)的render()函數(shù)
/** * 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 ''; }
細(xì)心的讀者已經(jīng)發(fā)覺(jué),還有兩個(gè)繼承類,分別是SimpleThreePresenter和SimpleFourPresenter,既然是Simple(簡(jiǎn)單),區(qū)別就在他們的render()函數(shù)
/** * 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 ''; }
也就是說(shuō),SimpleThreePresenter和SimpleFourPresenter生成的分頁(yè)按鈕是沒(méi)有“頁(yè)碼”的,只有“上一頁(yè)”和“下一頁(yè)”按鈕。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- laravel自定義分頁(yè)效果
- Laravel實(shí)現(xiàn)搜索的時(shí)候分頁(yè)并攜帶參數(shù)
- 在Laravel中實(shí)現(xiàn)使用AJAX動(dòng)態(tài)刷新部分頁(yè)面
- Laravel框架實(shí)現(xiàn)超簡(jiǎn)單的分頁(yè)效果示例
- Laravel框架搜索分頁(yè)功能示例
- Laravel框架執(zhí)行原生SQL語(yǔ)句及使用paginate分頁(yè)的方法
- laravel手動(dòng)創(chuàng)建數(shù)組分頁(yè)的實(shí)現(xiàn)代碼
- Laravel5.5 手動(dòng)分頁(yè)和自定義分頁(yè)樣式的簡(jiǎn)單實(shí)現(xiàn)
相關(guān)文章
Uchome1.2 1.5 代碼學(xué)習(xí) common.php
這是uchome 用到的php代碼,大家在學(xué)習(xí)編程的時(shí)候,可以看下當(dāng)時(shí)比較成熟的一些cms系統(tǒng),前提是你對(duì)程序比較熟悉啊,要不看了會(huì)打消學(xué)習(xí)積極性的。2009-04-04php獲取小程序碼的實(shí)現(xiàn)代碼(B類接口)
這篇文章主要介紹了php獲取小程序碼的實(shí)現(xiàn)代碼(B類接口),需要的朋友可以參考下2020-06-06自己寫的php curl庫(kù)實(shí)現(xiàn)整站克隆功能
這篇文章主要介紹了自己寫的php curl庫(kù)實(shí)現(xiàn)整站克隆功能,本文給出工具庫(kù)源碼下載和使用示例,需要的朋友可以參考下2015-02-02laravel使用redis隊(duì)列實(shí)例講解
這篇文章主要介紹了laravel使用redis隊(duì)列實(shí)例講解,使用laravel框架之后配置redis還是很簡(jiǎn)單的,有感興趣的同學(xué)可以學(xué)習(xí)下2021-03-03Mac版PhpStorm之XAMPP整合apache服務(wù)器配置的圖文教程詳解
選擇在PhpStorm集成apache服務(wù)器,但是很多朋友不知道是如何操作的,下面小編分步驟通過(guò)圖文的形式給大家介紹Mac版PhpStorm之XAMPP整合apache服務(wù)器配置的教程,感興趣的朋友一起看看吧2016-10-10php 文件上傳至OSS及刪除遠(yuǎn)程阿里云OSS文件
今天給大家介紹php 文件上傳至OSS及刪除遠(yuǎn)程阿里云OSS文件的方法,在刪除文件的時(shí)候大家記住千萬(wàn)不要帶域名,具體操作示例參考下本文2021-07-07