vue 使用vue-scroller 列表滑动到底部加载更多数据

安装插件 npm install vue-scroller -d

main.js
import VueScroller from 'vue-scroller'
Vue.use(VueScroller)

<template>
  <div class="wrap">
    <div class="footer">
      <div class="btn"  @click="open = true">新增</div>
    </div>

    <div class="scrollerWrap">
      <scroller :on-infinite="infinite" ref="myscroller" :noDataText="noDataText" class="item">
        <div class="scrollerContent">
          <div class="list" v-for="(item,i) in reportRecordList" :key="i">
            <div class="cont">{{ item.name }}</div>
            <div class="list-bot">          
              <div class="list-del" @click="handleDelete(i)">删除</div>
              <div class="list-edit" @click="handleUpdate(i)">编辑</div>
            </div>
          </div>
        </div>
      </scroller>
    </div>

    <div class="mask" v-if="open"></div>
    <div class="pop-box" v-if="open">
      <div class="textare-wrap">
        <el-input type="textarea" v-model="oname" class="otextare" resize="none"></el-input>
      </div>
      <div class="pop-footer">
        <div class="b1" @click="cancel">取消</div>
        <div class="b2" @click="submitForm">保存</div>
      </div>
    </div>

  </div>
</template>

data里
      oname: '',
      // 是否显示弹出层
      open: false,
      // 用户信息
      user: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      // 体测数据记录目录表格数据
      reportRecordList: [],
      noDate: false,//这是一个判断是否加载的开关
      noDataText: '',//目前这个还没显示出来


methods里:
infinite(done) {
      let that = this;
      console.log(that.noDate)
      if (!that.noDate) {
        setTimeout(() => {          
          that.getList(done);
        }, 300)
      } else {
        this.noDataText ="无更多数据"
        that.$refs.myscroller.finishInfinite(true);//这个方法是不让它加载了,显示“没有更多数据”,要不然会一直转圈圈
      }
    },

    /** 查询体测数据记录目录列表 */
    getList(fn) {
      this.loading = true;
      listReportRecord(this.queryParams).then(response => {
        //this.reportRecordList = response.rows;
        let newDataList = this.reportRecordList.concat(response.rows)
        this.reportRecordList = newDataList
        this.total = response.total;
        console.log("---")
        console.log(newDataList.length >= this.total)
        if (newDataList.length >= this.total) {    
          if(fn) fn(true);      
          this.noDate = true
        } else {
          if(fn) fn();
          this.queryParams.pageNum++;//下拉一次页数+1
          this.noDate = false
        }
        this.loading = false;
      });
    },

<style scoped>
.wrap{ min-height: 100%; background: #f8f8f8;}
.list{ margin: 0 15px 10px 15px; padding: 15px; box-sizing: border-box; background: #fff; font-size: 14px; border-radius: 10px;}
.list-bot{ margin-top: 15px; display: flex; justify-content: flex-end;}
.list-edit,.list-del{ margin-left: 15px; padding: 0 15px; height: 30px; line-height: 30px; border: 1px solid #ddd; border-radius: 15px;}
.list-del{ color: #ccc;}
.cont{ color: #333;}
.footer{ position: fixed; bottom: 0; left: 0; z-index: 99; display: flex; justify-content: center; align-items: center; width: 100%; height: 50px; background: #fff; border-top: 1px solid #f8f8f8;}
.footer .btn{ padding: 0 20px; height: 36px; line-height: 36px; background: #33a7ff; font-size: 16; color: #fff; border-radius: 18px;}

.mask{ position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 998; background: rgba(0,0,0,0.3);}
.pop-box{ position: fixed; top: 50%; left: 50%; z-index: 999; transform: translate(-50%, -50%); box-sizing: border-box; width: 90%; background: #fff; border-radius: 10px;}
.pop-footer{ display: flex; height: 50px; border-top: 1px solid #f8f8f8;}
.otextare{ padding: 15px 15px 10px 15px}
.otextare /deep/ .el-textarea__inner{ height: 200px;}
.pop-footer .b1,.pop-footer .b2{ flex: 1; text-align: center; line-height: 50px; font-size: 16px; }
.pop-footer .b1{ border-right: 1px solid #f8f8f8; color: #999;}
.pop-footer .b2{ color: #33a7ff;}

.scrollerWrap{
    position:absolute;
    width:100%;
    height: calc(100% - 70px);
    top: 10px;
    bottom: 0;    
  }
  .scrollerContent{ height: 100%;}
</style>


个人记录 仅供参考

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现滑动分页列表,可以使用Vue.js配合一些插件来完成,以下是一些步骤: 1. 安装Vue.js并创建Vue实例; 2. 引入Vue插件vue-infinite-loading,它提供了无限滚动加载数据的功能; 3. 创建一个包含列表数据的组件,使用v-for指令来渲染列表; 4. 在组件中使用vue-infinite-loading的指令,在滚动到底部时自动加载下一页数据; 5. 监听滚动事件,当滚动到底部时触发加载数据; 6. 在组件中使用CSS样式来实现滑动效果。 以下是一个简单的示例代码: ```html <template> <div class="list-container" ref="listContainer"> <div v-for="(item, index) in listData" :key="index" class="list-item">{{ item }}</div> <div v-infinite-loading="loadMore" :infinite-disabled="isLoading" infinite-distance="10%"> <div v-if="isLoading"><i class="fa fa-spinner fa-spin"></i> 加载中...</div> <div v-else><i class="fa fa-arrow-down"></i> 下拉加载更多</div> </div> </div> </template> <script> import InfiniteLoading from 'vue-infinite-loading'; export default { components: { InfiniteLoading, }, data() { return { listData: [], currentPage: 1, isLoading: false, }; }, mounted() { this.loadData(); window.addEventListener('scroll', this.handleScroll); }, methods: { loadData() { // 模拟异步加载数据 this.isLoading = true; setTimeout(() => { for (let i = 0; i < 10; i++) { this.listData.push(`Item ${this.listData.length + 1}`); } this.currentPage++; this.isLoading = false; }, 1000); }, loadMore() { this.loadData(); }, handleScroll() { const container = this.$refs.listContainer; const scrollTop = container.scrollTop; const scrollHeight = container.scrollHeight; const clientHeight = container.clientHeight; if (scrollTop + clientHeight >= scrollHeight) { this.loadData(); } }, }, }; </script> <style> .list-container { height: 300px; overflow-y: scroll; } .list-item { height: 50px; line-height: 50px; border-bottom: 1px solid #eee; } </style> ``` 在上面的示例中,我们使用vue-infinite-loading插件来实现滑动分页列表的功能。同时,我们还监听了滚动事件,在滚动到底部时触发自动加载下一页数据。通过CSS样式设置了列表的高度和滑动效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值