一.分页功能
1.不管是数据库操作还是模型操作,都使用paginate()方法来实现(第一种方式);
//查找user表所有数据,每页显示5条
returnView::fetch('index', [
'list' => User::paginate(5)
]);
页数:
2.创建一个静态模版页面,并使用{volist}标签遍历列表;
<table border="1">
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>邮箱</th>
<th>价格</th>
</tr>
{volist name='list' id='user'}
<tr>
<td>{$user.id}</td>
<td>{$user.username}</td>
<td>{$user.gender}</td>
<td>{$user.email}</td>
<td>{$user.price}</td>
</tr>
{/volist}
</table>
3.分页功能还提供了一个固定方式,实现分页按钮,只需要设置相应的CSS即可;
{$list|raw}
<ul class="pagination">
.pagination {
list-style: none;
margin: 0;
padding: 0;
}
.pagination li {
display: inline-block;
padding: 20px;
}
4. 我们可以通过数组来传递多个参数(第二种方式),具体分页参数如下:
$list = User::paginate([
'list_rows' => 4,
'var_page' => 'page',
]);
可以通过var_page来改变网址页数的变量。
5. 也可以单独赋值分页的模版变量;
// 获取分页显示
$page = $list->render();
{$page|raw}
6. 也可以单独获取到总记录数量;
$total = $list->total();
7. 如果你使用模型方式分页,则可以通过获取器修改字段值,而分页本身也可以;
->each(function ($item, $key) {
$item['gender'] = '【'.$item['gender'].'】';
return $item;
});
8. 可以限定总记录数,比如,限定总记录数只有10条的页码;
->paginate(5, 10);
9. 也可以设置分页的页码为简洁分页,就是没有1,2,3,4这种,只有上下页;
->paginate(5, true);