本文实例讲述了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框架的生命周期与原理分析
laravel框架的路由设置
以上就是laravel框架实现分页的分析的详细内容。
