博客列表 >layui如何配合thinkphp5做分页

layui如何配合thinkphp5做分页

梁凯达的博客
梁凯达的博客原创
2020年07月06日 20:20:531658浏览

最近用layui和tp5做分页,遇到了不少的坑,因为太久没有写代码了,很多概念上的东西过于生疏,这里把一些知识点再复习一下:
首先,layui有明确的分页方法,我们一般使用layui的表格分页,表格分页。在layui的内置模块->表格中有明确的分类方法

  1. table.render({
  2. elem: '#demo'
  3. ,height: 312
  4. ,url: '/demo/table/user/' //数据接口
  5. ,page: true //开启分页
  6. ,cols: [[ //表头
  7. {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
  8. ,{field: 'username', title: '用户名', width:80}
  9. ,{field: 'sex', title: '性别', width:80, sort: true}
  10. ,{field: 'city', title: '城市', width:80}
  11. ,{field: 'sign', title: '签名', width: 177}
  12. ,{field: 'experience', title: '积分', width: 80, sort: true}
  13. ,{field: 'score', title: '评分', width: 80, sort: true}
  14. ,{field: 'classify', title: '职业', width: 80}
  15. ,{field: 'wealth', title: '财富', width: 135, sort: true}
  16. ]]
  17. });

我们从内置的模块当中就可以看到,里面有一个page的方法,方法为true时,代表开启分页,但是此处的分页,是需要插入layui的分页组件的,在layuipage中,有对组件有说明,我们需要把page以数组的形式插入其中,比如:

  1. table.render({
  2. elem: '#demo'
  3. ,height: 312
  4. ,url: '/demo/table/user/' //数据接口
  5. ,page:{
  6. layout:['limit','count','prev','page','next','skip']
  7. //layout是自定义布局
  8. ,limit:1
  9. ,limits:[30,50,100]//每页显示多少
  10. ,groups:5 //显示几个连续页码
  11. ,first:'首页'
  12. last:'尾页'
  13. }
  14. //插入一个数组,参数是lauuipage中的参数
  15. ,cols: [[ //表头
  16. {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
  17. ,{field: 'username', title: '用户名', width:80}
  18. ,{field: 'sex', title: '性别', width:80, sort: true}
  19. ,{field: 'city', title: '城市', width:80}
  20. ,{field: 'sign', title: '签名', width: 177}
  21. ,{field: 'experience', title: '积分', width: 80, sort: true}
  22. ,{field: 'score', title: '评分', width: 80, sort: true}
  23. ,{field: 'classify', title: '职业', width: 80}
  24. ,{field: 'wealth', title: '财富', width: 135, sort: true}
  25. ]]
  26. });

上面进行了laytable中的参数设置,这里需要注意的是:
1/在php中进行数据返回,返回的数据为一个2维数组
2/method请求默认为get格式,get格式用于传递参数
3/
规定的返回数据样式如下:

  1. {
  2. "code": 0,
  3. "msg": "",
  4. "count": 1000,
  5. "data": [{}, {}]
  6. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议