【Uniapp小程序】实现上滑加载分页(触底加载)附后端Thinkphp代码

简介: 【Uniapp小程序】实现上滑加载分页(触底加载)附后端Thinkphp代码

分析说明

后端

$query_total=DB::table('book')->where(["class_id"=>$class_id])->paginate($per_page);
 查询符合条件的总记录总数
 $class=DB::table('book')->where(["class_id"=>$class_id])->limit($page,$per_page)->select();
 查询limit限制后的数据
 注意:thinkphp中 limit(a,b)表示从第a行的b条数据
//依据分类查询图书
    public function query_book_by_classid(){
        $token=input('token');
        $class_id=input('class_id');
        $page=input('page');//起始行
        $per_page=input('per_page');//每页数据
        $redis = new Redis();
        $result= $redis->get($token);
       if ($result) {
        //   limit(a,b)从第a行的b条数据 
              $query_total=DB::table('book')->where(["class_id"=>$class_id])->paginate($per_page);
              $class=DB::table('book')->where(["class_id"=>$class_id])->limit($page,$per_page)->select();
              $coun=ceil($query_total->total()/$per_page);
        die(
        json_encode(
            array(
            'code' => 200,
            'total' => $coun,
            'data' => $class,
            'msg' => '获取成功'
        ),480)
);
       } 
       else {
           die(
        json_encode(
            array(
            'code' => 100,
            'data'=>'',
            'msg' => 'token失效或不存在!请重新获取'
        ),480)
);
       }
    }

小程序端

实现触底加载我们需要用到onReachBottom方法

onReachBottom() {
    console.log("我被触发了");
    },
<script>
  export default {
    data() {
      return {
        // 商品数据
        goodsList:[],
        loadText:"",
        loadSwitch:false,
        // 分页信息
        total:"",//总页数
        page:0,//起始页
        per_page:6,//每页显示
      }
    },
    onReachBottom() {
      // 如果当前页数大于等于总页数,状态修改为没有更多了,不再继续往下执行代码
            uni.showLoading({
              title:'加载中..'
            })
          if(this.loadSwitch){
            this.loadSwitch = false;
            this.page = this.page+this.per_page;//页面新增一页
          setTimeout(() => {
            this.getGoodsList(this.tabs2[0]['id']);
          }, 1200); 
          }else{
            uni.hideLoading()
          }
        },
    onLoad() {
      this.get_book_class();
      setTimeout(() => {
        this.getGoodsList(this.tabs2[0]['id']);     
        console.log(this.tabs2[0]['id'])
      }, 1100);
    },
    methods: {
      getGoodsList(ee){
        console.log("默认得到的分类id"+ee)
              this.loadText = "加载中";
              uni.request({
                url: 'https://xxxxx/index.php/index/Api/query_book_by_classid', //仅为示例,并非真实接口地址。
                data: {
                    token:uni.getStorageSync('token'),
                  class_id:ee,
                  page:this.page,
                  per_page:this.per_page
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
                },
                success: (res) => {
                  this.goodsList = this.goodsList.concat(res.data.data);
                  var total=res.data.total;
                  if(this.page >= total) {
                    this.loadSwitch = false;
                    this.loadText = "没有啦~";
                    uni.hideLoading()
                  }else{
                  setTimeout(()=>{
                    this.loadText = "上拉加载更多";
                    this.loadSwitch = true;
                    uni.hideLoading()
                  },200);
                  }
                    }
              })
            },
      get_book_class(){
        let url = 'https://xxxxx/index.php/index/Api/query_class';
          uni.request({
            url,
            data: {
               token:uni.getStorageSync('token')
            },
            method:'GET',
            success: (res) => {
              console.log(res.data)
              if (res.data.code==200) {
                console.log(res.data.data.length)
                for(let i = 0;i<res.data.data.length;i++){
                  this.tabs2.push(res.data.data[i]);
                }
              } else{
                uni.showToast({
                  title:res.data.msg,
                  icon:'none'
                })
              }
              }
          })
      },
      onTabs2Change(index){
        // 设置激活
        console.log(index)
        this.tabs2Current = index;
        // 设置滚动位置
        let into = 0;
        if(this.tabs2.length - 2 < index){
          into = this.tabs2.length - 1;
        }else if(index > 1){
          into = index - 2;
        }
        this.tabs2IntoView = `tabs-${into}`
      }
    }
  }
</script>

代码成品

相关文章
|
14天前
|
存储 小程序 前端开发
微信小程序与Java后端实现微信授权登录功能
微信小程序极大地简化了登录注册流程。对于用户而言,仅仅需要点击授权按钮,便能够完成登录操作,无需经历繁琐的注册步骤以及输入账号密码等一系列复杂操作,这种便捷的登录方式极大地提升了用户的使用体验
139 12
|
1月前
|
移动开发 小程序
thinkphp+uniapp开发的多端商城系统源码/H5/小程序/APP支持DIY模板直播分销
thinkphp+uniapp开发的多端商城系统源码/H5/小程序/APP支持DIY模板直播分销
28 0
|
3月前
|
人工智能 小程序 搜索推荐
uni app下开发AI运动小程序解决方案
本文介绍了在小程序中实现AI运动识别的解决方案。该方案依托于UNI平台,通过高效便捷的插件形式,实现包括相机抽帧控制、人体识别、姿态识别等在内的多项功能,无需依赖后台服务器,大幅提高识别效率和用户体验。方案内置多种运动模式,支持自定义扩展,适用于AI健身、云上赛事、AI体测等多场景,适合新开发和存量改造项目。
|
3月前
|
小程序 前端开发 JavaScript
在线课堂+工具组件小程序uniapp移动端源码
在线课堂+工具组件小程序uniapp移动端源码
82 0
在线课堂+工具组件小程序uniapp移动端源码
|
3月前
|
小程序 前端开发 算法
|
4月前
|
移动开发 小程序 数据可视化
基于npm CLI脚手架的uniapp项目创建、运行与打包全攻略(微信小程序、H5、APP全覆盖)
基于npm CLI脚手架的uniapp项目创建、运行与打包全攻略(微信小程序、H5、APP全覆盖)
576 3
|
4月前
|
小程序 API
微信小程序更新提醒uniapp
在小程序开发中,版本更新至关重要。本方案利用 `uni-app` 的 `uni.getUpdateManager()` API 在启动时检测版本更新,提示用户并提供立即更新选项,自动下载更新内容,并在更新完成后重启小程序以应用新版本。适用于微信小程序,确保用户始终使用最新版本。以下是实现步骤: ### 实现步骤 1. **创建更新方法**:在 `App.vue` 中创建 `updateApp` 方法用于检查小程序是否有新版本。 2. **测试**:添加编译模式并选择成功状态进行模拟测试。
96 0
微信小程序更新提醒uniapp
|
4月前
|
移动开发 小程序 数据可视化
一招学会DIY官网可视化设计支持导出微擎、UNIAPP、H5、微信小程序源码
一招学会DIY官网可视化设计支持导出微擎、UNIAPP、H5、微信小程序源码
80 2
|
4月前
|
小程序 数据可视化 API
低代码可视化-uniapp商城首页小程序-代码生成器
低代码可视化-uniapp商城首页小程序-代码生成器
47 0
|
4月前
|
存储 移动开发 小程序
uniapp富文本editor输入二次扩展兼容微信小程序
uniapp富文本editor输入二次扩展兼容微信小程序
170 0

热门文章

最新文章