17.thinkphp的分页功能

一.分页功能

1.不管是数据库操作还是模型操作,都使用paginate()方法来实现(第一种方式);

php 复制代码
//查找user表所有数据,每页显示5条
returnView::fetch('index', [
 'list' => User::paginate(5)
 ]);

页数:

2.创建一个静态模版页面,并使用{volist}标签遍历列表;

php 复制代码
<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即可;

php 复制代码
{$list|raw}
 <ul class="pagination">
 .pagination {
 list-style: none;
 margin: 0;
 padding: 0;
 }
 .pagination li {
 display: inline-block;
 padding: 20px;
 }
  1. 我们可以通过数组来传递多个参数(第二种方式),具体分页参数如下:
php 复制代码
$list = User::paginate([
 'list_rows' => 4,
 'var_page' => 'page',
 ]);

可以通过var_page来改变网址页数的变量。

  1. 也可以单独赋值分页的模版变量;
php 复制代码
// 获取分页显示
$page = $list->render();
 {$page|raw}
  1. 也可以单独获取到总记录数量;
php 复制代码
$total = $list->total();
  1. 如果你使用模型方式分页,则可以通过获取器修改字段值,而分页本身也可以;
php 复制代码
->each(function ($item, $key) {
 $item['gender'] = '【'.$item['gender'].'】';
 return $item;
 });
  1. 可以限定总记录数,比如,限定总记录数只有10条的页码;
php 复制代码
->paginate(5, 10);
  1. 也可以设置分页的页码为简洁分页,就是没有1,2,3,4这种,只有上下页;
php 复制代码
->paginate(5, true);
相关推荐
呐谁吃饭了1 天前
网络安全学习-6
前端
北辰alk1 天前
深入剖析Redux中间件实现原理:从概念到源码
前端
Pedantic1 天前
SwiftUI ObservableObject 观察者模式学习笔记
前端
跟橙姐学代码1 天前
列表、元组与字典:Python开发者的三大必备利器,再向高手靠近一步
前端·python·ipython
蜗牛快跑1231 天前
拆巨资让 Claude Code 和 Codex 同时住进了我的终端里
前端·后端·ai编程
小高0071 天前
🔥🔥🔥Vue部署踩坑全记录:publicPath和base到底啥区别?99%的前端都搞错过!
前端·vue.js·面试
摸着石头过河的石头1 天前
HTTP内容类型:从基础到实战的全方位解析
前端·http·浏览器
luckyzlb1 天前
02- html && css
前端·css·html
AI@独行侠1 天前
04 - 【HTML】- 常用标签(下篇)
前端·html