当前位置: 移动技术网 > IT编程>开发语言>PHP > Laravel手动分页实现方法详解

Laravel手动分页实现方法详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

pep小学英语,冒充物业抢劫杀人,广州天气2345

本文实例讲述了laravel手动分页实现方法。分享给大家供大家参考,具体如下:

这里的演示实例基于laravel的5.2版本

在开发过程中有这么一种情况,你请求java api获取信息,由于信息较多,需要分页显示。laravel官方提供了一个简单的方式paginate($perpage),但是这种方法只适用model、查询构建器。

今天说下 给定一个数组如何实现 和paginate方法一样的效果。

查看paginate方法源码

#vendor/laravel/framework/src/illuminate/database/eloquent/builder.php:480
public function paginate($perpage = null, $columns = ['*'], $pagename = 'page', $page = null)
{
    $query = $this->tobase();
    $total = $query->getcountforpagination();
    $this->forpage(
      $page = $page ?: paginator::resolvecurrentpage($pagename),
      $perpage = $perpage ?: $this->model->getperpage()
    );
    return new lengthawarepaginator($this->get($columns), $total, $perpage, $page, [
      'path' => paginator::resolvecurrentpath(),
      'pagename' => $pagename,
    ]);
}

从上面就可以看出,分页的关键就在于lengthawarepaginator。

lengthawarepaginator的构造方法。

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 = (int) ceil($total / $perpage);
    $this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path;
    $this->currentpage = $this->setcurrentpage($currentpage, $this->lastpage);
    $this->items = $items instanceof collection ? $items : collection::make($items);
}

其实已经很明白了,假如要分页的数组为

[
  ['username'=>'zhangsan', 'age'=>26],
  ['username'=>'lisi', 'age'=>23],
  ['username'=>'wangwu', 'age'=>62],
  ['username'=>'zhaoliu', 'age'=>46],
  ['username'=>'wangmazi', 'age'=>25],
  ['username'=>'lanzi', 'age'=>24],
  ['username'=>'pangzi', 'age'=>21],
]

共7条数据,每页显示3条,共3页

use illuminate\pagination\lengthawarepaginator;
use illuminate\pagination\paginator;
use illuminate\http\request;
# 仅做演示 #
function userlist(request $request) {
  $users = [
    ['username'=>'zhangsan', 'age'=>26],
    ['username'=>'lisi', 'age'=>23],
    ['username'=>'wangwu', 'age'=>62],
    ['username'=>'zhaoliu', 'age'=>46],
    ['username'=>'wangmazi', 'age'=>25],
    ['username'=>'lanzi', 'age'=>24],
    ['username'=>'pangzi', 'age'=>21]
  ];
  $perpage = 3;
  if ($request->has('page')) {
      $current_page = $request->input('page');
      $current_page = $current_page <= 0 ? 1 :$current_page;
  } else {
      $current_page = 1;
  }
  $item = array_slice($users, ($current_page-1)*$perpage, $perpage); //注释1
  $total = count($users);
  $paginator =new lengthawarepaginator($item, $total, $perpage, $currentpage, [
      'path' => paginator::resolvecurrentpath(), //注释2
      'pagename' => 'page',
  ]);
  $userlist = $paginator->toarray()['data'];
  return view('userlist', compact('userlist', 'paginator'));
}

上面的代码中的重点是$item,如果不做注释1处理,得出的是所有7条数据。

注释2处就是设定个要分页的url地址。也可以手动通过 $paginator ->setpath('路径') 设置。

页面中的分页连接也是同样的调用方式:

{{ $paginator->render() }}

好了,基本就是这样,有纰漏的地方欢迎指正!

看看最终效果:

更多关于laravel相关内容感兴趣的读者可查看本站专题:《laravel框架入门与进阶教程》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于laravel框架的php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网