Laravel5.5 手动分页和自定义分页样式的简单实现
发布:smiling 来源: PHP粉丝网 添加日期:2022-01-04 20:17:46 浏览: 评论:0
今天小编就为大家分享一篇Laravel5.5 手动分页和自定义分页样式的简单实现,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。
基于Laravel5.5 在项目实施过程中,需要对从接口中获取的数据(或者通过搜索工具查询出来的数据)进行分页
一、创建手动分页
在laravel自带的分页中,一般是通过数据库查询访问paginate()方法来达到分页的效果 ,like this:
class IndexControllerextends Controller
- {
- publicfunctionindex()
- {
- $person = DB::table('person')->paginate(15);
- return view('index.pagTest',['person'=> $person]);
- }
- }
查看框架的分页源代码
#vender/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php
- /**
- * Paginate the given query.
- *
- * @param int $perPage
- * @param array $columns
- * @param string $pageName
- * @param int|null $page
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
- *
- * @throws \InvalidArgumentException
- */
- public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
- {
- $page = $page ?: Paginator::resolveCurrentPage($pageName);
- $perPage = $perPage ?: $this->model->getPerPage();
- $results = ($total = $this->toBase()->getCountForPagination())
- ? $this->forPage($page, $perPage)->get($columns)
- : $this->model->newCollection();
- return $this->paginator($results, $total, $perPage, $page, [
- 'path' => Paginator::resolveCurrentPath(),
- 'pageName' => $pageName,
- ]);
- }
发现,分页用了 \Illuminate\Contracts\Pagination\LengthAwarePaginator 构造方法,查看这个构造方法
- <?php
- namespace Illuminate\Pagination;
- use Countable;
- use ArrayAccess;
- use JsonSerializable;
- use IteratorAggregate;
- use Illuminate\Support\Collection;
- use Illuminate\Support\HtmlString;
- use Illuminate\Contracts\Support\Jsonable;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
- class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorContract
- {
- /**
- * The total number of items before slicing.
- *
- * @var int
- */
- protected $total;
- /**
- * The last available page.
- *
- * @var int
- */
- protected $lastPage;
- /**
- * Create a new paginator instance.
- *
- * @param mixed $items
- * @param int $total
- * @param int $perPage
- * @param int|null $currentPage
- * @param array $options (path, query, fragment, pageName)
- * @return void
- */
- public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
- {
- foreach ($options as $key => $value) {
- $this->{$key} = $value;
- }
- $this->total = $total;
- $this->perPage = $perPage;
- $this->lastPage = max((int) ceil($total / $perPage), 1);
- $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
- $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
- $this->items = $items instanceof Collection ? $items : Collection::make($items);
- }
如果要实现手动分页,只需要使用这个构造方法,给定参数,就能达到分页的效果
贴代码:
- public function setPage2(Request $request,$data,$prepage,$total){
- #每页显示记录
- $prePage = $prepage;
- //$total =count($data);
- $allitem = $prepage *100;
- $total > $allitem ? $total = $allitem : $total;
- if(isset($request->page)){
- $current_page =intval($request->page);
- $current_page =$current_page<=0?1:$current_page;
- }else{
- $current_page = 1;
- }
- #url操作
- $url = $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
- if(strpos($url,'&page')) $url=str_replace('&page='.$request->page, '',$url);
- # $data must be array
- $item =array_slice($data,($current_page-1)*$prePage,$prePage);
- $paginator = new LengthAwarePaginator($item,$total,$prePage,$current_page,[
- 'path'=>$url,
- 'pageName'=>'page'
- ]);
- return $paginator;
- }
($data 为需要进行分页的数据)
说明:
1、在考虑到代码的复用性,我将分页代码封装到app/Controllers/Controller.php中的一个方法里面,这样在其他控制器里只需要$this->setPage(Request $request,$data,$prepage,$total) 就能使用了,(前提:其他控制器继承了Controller.php)
2、分页的URL,因为我的项目的url一定会携带一个kw参数,所以我直接用str_replace替换"&page",如果是存在不携参分页的话,需要判断,到底是"?page"还是"&page"。(url的逻辑可以自己写)
#分页 php
$paginator = $this->setPage2($request,$data,25,$sum);
$data =$paginator->toArray()['data'];
在模板中:{{$paginator->render()}}即能输出分页HTML,样式如下:
二、自定义分页样式
在实际开发中,不希望用户在浏览时直接浏览最后几页,只想用户从前往后依次的浏览,如百度搜索分页,这时候,就想修改分页的样式,经过一个下午的奋战,贴出解决过程
在上一环节中,手动创建了分页,了解HTML的模板生成是render()方法,
#\Illuminate\Contracts\Pagination\LengthAwarePaginator
- /**
- * Render the paginator using the given view.
- *
- * @param string|null $view
- * @param array $data
- * @return \Illuminate\Support\HtmlString
- */
- public function render($view = null, $data = [])
- {
- return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
- 'paginator' => $this,
- 'elements' => $this->elements(),
- ]))->render());
- }
经过思考,我们不去改laravel框架的源代码,可以通过重构render方法或者重新定义一个生成HTML模板的方法来实现自定义HTML模板
因为我们只需要自定义HTML模板,所以,可以创建一个文件,继承\Illuminate\Contracts\Pagination\LengthAwarePaginator 类
看代码:
- <?php
- namespace App\Helpers;
- use Illuminate\Pagination\LengthAwarePaginator;
- /**
- * Created by PhpStorm.
- * User: 1
- * Date: 2018/4/9
- * Time: 9:08
- */
- class Newpage extends LengthAwarePaginator {
- public $de_page = 10; //默认显示分页数
- public $pageHtml;
- public function newrender(){
- if($this->hasPages())
- {
- return sprintf("<ul class='pagination'>%s %s %s</ul>",
- $this->pre_page(),
- $this->pages_num(),
- $this->next_page()
- );
- }
- }
- #上一页
- public function pre_page(){
- if($this->currentPage == 1){
- //dd($this->currentPage);
- return "<li class='disabled'><span>《</span></li>";
- }else{
- $url = $this->path."&page=".($this->currentPage-1);
- //dd($url);
- return "<li><a href=".$url." rel="external nofollow" rel='prev'>《</a></li>";
- }
- }
- #页码
- public function pages_num(){
- $pages = '';
- if($this->currentPage <= 6){
- for($i = 1; $i <= $this->de_page; $i++){
- if($this->currentPage == $i){
- $pages .= "<li class='active'><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$i.">".$i."</a></li>";
- }else{
- $pages .="<li><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$i.">".$i."</a></li>";
- }
- }
- }else{
- #当前页前边部分
- for($i = 5; $i >=1 ; $i--){
- $url =$this->currentPage-$i;
- $pages .= "<li><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$url.">".$url."</a></li>";
- }
- #当前页
- $pages .= "<li class='active'><span>".$this->currentPage."</span></li>";
- #当前页后边部分
- for($i = 1;$i < 5; $i++ ){
- $nowpage =$this->currentPage+$i;
- $pages .= "<li><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$nowpage.">".$nowpage."</a></li>";
- }
- }
- return $pages;
- }
- #下一页
- public function next_page(){
- if($this->currentPage < $this->total){
- $page =$this->currentPage+1;
- return "<li><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$page." rel='next'><span>》</span></a></li>";
- }else{
- return "<li class='disabled'><span>》</span></li>";
- }
- }
- }
我选择的方法是自定义新的方法生成HTML模板,模板中通过:{{$paginator->newrender()}}输出HTML
如果选择重构render()方法,只需要将上面的newrender()方法做一些小变动
- public function render($view=null,$data=[]){
- if($this->hasPages())
- {
- return sprintf("<ul class='pagination'>%s %s %s</ul>",
- $this->pre_page(),
- $this->pages_num(),
- $this->next_page()
- );
- }
- }
模板中通过:{{$paginator->render()}}输出HTML
最终效果如图:
注意:自定义HTML后因为新建了一个类继承了LengthAwarePaginator类,需要将第一步手动分页的方法中new LengthAwarePaginator 修改为 new Newpage 参数不变。
Tags: Laravel5 5手动分页 自定义分页
相关文章
- ·如何在laravel 5中使用DB事务?(2020-01-31)
- ·Laravel5中contracts详解(2021-05-15)
- ·Laravel 5 学习笔记(2021-05-15)
- ·Laravel 5.5官方推荐的Nginx配置学习教程(2021-08-11)
- ·laravel5 使用try catch的实例详解(2021-08-21)
- ·Laravel5.5中利用Passport实现Auth认证的方法(2021-08-22)
- ·Laravel 5.5基于内置的Auth模块实现前后台登陆详解(2021-08-25)
- ·Laravel5.2使用Captcha生成验证码实现登录(session巨坑)(2021-08-26)
- ·Laravel 5.4.36中session没有保存成功问题的解决(2021-09-03)
- ·在Laravel5.6中使用Swoole的协程数据库查询(2021-10-01)
- ·详细Laravel5.5执行表迁移命令出现表为空的解决方案(2021-10-10)
- ·Laravel5.5以下版本中如何自定义日志行为详解(2021-10-20)
- ·Laravel5框架添加自定义辅助函数的方法(2021-10-20)
- ·laravel5使用freetds连接sql server的方法(2021-11-02)
- ·Laravel5.4框架使用socialite实现github登录的方法(2021-11-13)
- ·Laravel5.7框架安装与使用学习笔记图文详解(2021-11-14)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)