laravel二维数组手动分页显示
示例:数组 $user 如下
$user: array (size=5) 'sort' => array (size=71) 14 => float 0.028616622341171 15 => float 0.031555520560068 16 => float 0.030877620651548 17 => float 0.035307960620288 18 => float 0.030833821224056 19 => float 0.033605497937075 20 => float 0.024648793795052 'totalHours' => array (size=71) 14 => float 513.25 15 => float 565.96 16 => float 553.8 17 => float 633.26 18 => float 553.01 19 => float 602.72 20 => float 442.08 'days' => array (size=71) 14 => float 59 15 => float 62 16 => float 64 17 => int 69 18 => float 62 19 => float 60 20 => float 52.5 'dayHours' => array (size=71) 14 => float 8.7 15 => float 9.13 16 => float 8.65 17 => float 9.18 18 => float 8.92 19 => float 10.05 20 => float 8.42
分页调用
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; public function show(Request $request){ //手动分页 $users = $kaoqin; $perPage = 10; 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, $current_page, [ 'path' => Paginator::resolveCurrentPath(), //注释2 'pageName' => 'page', ]); $userlist = $paginator->toArray()['data']; return view('web.attendance.groupList',compact('userlist', 'paginator')); }
html页面显示:
<body > <table width="100%" class="table table-hover"> <tr class="info"> <th style="align-content: center;width: 15%">姓名</th> <th style="align-content: center;width: 30%">时间</th> <th style="align-content: center;width: 15%">状态</th> <th style="align-content: center;width: 40%">原因</th> </tr> @if(!empty($userlist)) @foreach($userlist as $person) <tr > <td style="vertical-align: middle;">{{$person['name']}}</td> <td style="vertical-align: middle;">{{$person['time']}}</td> <td style="vertical-align: middle;">{{$person['status']}}</td> <td style="vertical-align: middle;">{!! $person['reason'] !!}</td> </tr> @endforeach @endif </table> <div class="text-center"> {{ $paginator->links() }} </div> </body> </html>
上面的代码中的重点是$item,如果不做注释1处理,得出的是所有数据。
注释2处就是设定个要分页的url地址。也可以手动通过 $paginator ->setPath(‘路径’) 设置。
页面中的分页连接也是同样的调用方式 {{ $paginator->render() }}