前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ThinkPHP5 另类方法实现分页功能

ThinkPHP5 另类方法实现分页功能

作者头像
紫旭Blog - blog.zixutech.cn
发布2019-12-30 22:48:17
6460
发布2019-12-30 22:48:17
举报
文章被收录于专栏:紫旭紫旭

前言

当然是自己需要这个功能啦。。。

准备

我所用的前端框架是老外用BootStrap4二开的主题,叫MaterialPro(以下简称MP),我会在本文末附上压缩包。非常好用哦。

传统的分页是使用ul li来做,但是最大的问题就是如果没有正好的样式,那么你还得费大半天时间去写样式,烦得很。所以我这次使用的是MP的按钮组,美观也好看。

实现方法

分页实现是用的TP5自带的paginate方法,在Model里查询数据的时候直接使用该方法进行分页。然后将对象返回过来就好。

注意:官方文档写的是使用render方法来分页,但是在这里我们不用这个方法,因为他在我这有各种BUG。

将数据对象返回过来之后,var_dump之后结构是这样的(这里只发出来跟分页有关的数据结构)

代码语言:javascript
复制
protected 'currentPage' => int 1
  protected 'lastPage' => int 1
  protected 'total' => int 4
  protected 'listRows' => int 10
  protected 'hasMore' => boolean false
  protected 'options' => 
    array (size=6)
      'var_page' => string 'page' (length=4)
      'path' => string '/index/index/charge' (length=19)
      'query' => 
        array (size=0)
          empty
      'fragment' => string '' (length=0)
      'type' => string 'bootstrap' (length=9)
      'list_rows' => int 15
  protected 'nextItem' => null

提示:这里很多人会误认为total是总页数,其实是错误的,total是数据总条数。

接下来就是前端了,很easy,我直接把代码贴上来你们读一下就懂了

代码语言:javascript
复制
<div class="float-right">
    {if $record->currentPage()>1}
    <button id="pre" type="button" class="btn btn-secondary" data-page="/index/index/charge?page={$record->currentPage()-1}">上一页</button>
    {/if}
    <div class="btn-group" role="group">
        {for start="1" end="$record->lastPage()+1" name="page"}
        <button type="button" class="pages btn {if $page==$record->currentPage()}btn-info{else /}btn-secondary{/if}" data-page="/index/index/charge?page={$page}">{$page}</button>
        {/for}
    </div>
    {if $record->currentPage()<$record->lastPage()}
    <button id="af" type="button" class="btn btn-secondary" data-page="/index/index/charge?page={$record->currentPage()+1}">下一页</button>
    {/if}
</div>
<!--这里写分页代码-->

以及js代码

代码语言:javascript
复制
$('#pre').on('click',function () {
    window.location.href = $(this).attr('data-page');
});
$('#af').on('click',function () {
    window.location.href = $(this).attr('data-page');
});
$('.pages').on('click',function () {
    window.location.href = $(this).attr('data-page');
});
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-10-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 准备
  • 实现方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档