使用Vant如何实现数据分页,下拉加载

 更新时间:2022年06月28日 15:24:11   作者:倘若hfl  
这篇文章主要介绍了使用Vant实现数据分页及下拉加载方式。具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Vant-ui的van-list实现数据分页加载

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vant数据分页,下拉加载</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.11/lib/index.css" rel="external nofollow"  />
</head>
<style>
</style>
<body>
  <div id='app'>
    <van-list class="lazy" v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad"
      :immediate-check="false">
      <div v-for="(item,index) in list" :key="index">{{item}}</div>
    </van-list>
  </div>
</body>
<script src="https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vant@2.11/lib/vant.min.js"></script>
<script>
  var Vue = new Vue({
    el: '#app',
    data: {
      list: [],
      page: 1,
      loading: false,
      finished: false,
      num: 0
    },
    created() {
      this.getList()
    },
    mounted() {
    },
    methods: {
      // 请求公共方法
      ajax(url, params, cb) {
        $.ajax({
          type: 'post',
          url: url,
          data: params,
          dataType: "json",
          success: function (response) {
            cb(response)
          }
        });
      },
      onLoad() {
        this.getList()
      },
      getList() {
        let that = this
        that.ajax('url', { kay: 'value' }, function (res) {
          if (res.errcode != 0) {
            that.$toast(res.msg)
            return false
          }
          if (that.page == 1) {
            that.list = res.data.list
          } else {
            that.list = that.list.concat(res.data.list)
          }
          that.loading = false;
          that.page++
          //最后一次请求返回的数据为空或小于10条,不在请求,finished = true
          //根据业务需求更改
          if (res.data.list.length == 0 || res.data.list == null || res.data.list.length < 10) {
            that.finished = true
            return
          }
        })
      }
    }
  })
</script>
</html>

主要三个属性

注意:

  • v-model 每次数据加载完成要置为false
  • finished 置为false后将不再触发下拉加载
  • immediate-check 置为false后,每次进入页面将不会触发load方法,防止进入页面多次加载

vant上拉加载更多,下拉刷新

1.html

   <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
            <van-list
              v-model="loading"
              :finished="finished"
              :immediate-check="false"
              finished-text="没有更多了呦"
              @load="onLoad"
            > 
         
            </van-list>
          </van-pull-refresh>

2.js

 return {    
      isLoading: false,
      loading: false,   
    
      page: 1,
      limit: 10,
      finished: false,
      total: 0, // 总共的数据条数
      List: [], 
    }
 
   getHistory() {
      const historyData = {
        page: this.page,
        limit: this.limit
      }
      return new Promise((resolve, reject) => {
        getHistory(historyData)
          .then(res => {
            if (res.code === 0) {
              console.log(res, '历史记录')
              this.total = res.data.total
              this.finished = !res.data.hasNext
              if (res.data.list && res.data.list.length > 0) {
                const tempList = res.data.list
                // console.log(this.page)
                if (this.page > 1) {
                  this.list = this.list.concat(tempList)
                } else {
                  this.list = tempList // 第一次加载
                }
                this.page += 1
              } else {
                this.list = []
              }
              this.loading = false
              resolve()
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    }, 
 
  onLoad() {
      this.getHistory()
    },
    onRefresh() {
      this.page = 1
      setTimeout(() => {
        this.getHistory()
        Toast('刷新成功')
        this.isLoading = false
      }, 1000)
    },

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

相关文章

  • vue实现a标签点击高亮方法

    vue实现a标签点击高亮方法

    下面小编就为大家分享一篇vue实现a标签点击高亮方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • Vue quill-editor 编辑器使用及自定义toobar示例详解

    Vue quill-editor 编辑器使用及自定义toobar示例详解

    这篇文章主要介绍了Vue quill-editor编辑器使用及自定义toobar示例详解,这里讲解编辑器quil-editor的知识结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • vue中 render 函数功能与原理分析

    vue中 render 函数功能与原理分析

    这篇文章主要介绍了vue中 render 函数功能与原理,结合实例形式分析了vue中render函数的基本功能、原理及相关操作注意事项,需要的朋友可以参考下
    2023-06-06
  • 详细对比Ember.js和Vue.js

    详细对比Ember.js和Vue.js

    这篇文章主要介绍了详细对比Ember.js和Vue.js,对JS框架感兴趣的同学,可以参考下
    2021-05-05
  • vue中页面跳转拦截器的实现方法

    vue中页面跳转拦截器的实现方法

    这篇文章主要给大家介绍了关于vue中页面跳转拦截器的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
    2017-08-08
  • 一文带你搞懂Vue3 defineModel中的双向绑定

    一文带你搞懂Vue3 defineModel中的双向绑定

    随着vue3.4版本的发布,defineModel也正式转正了,它可以简化父子组件之间的双向绑定,是目前官方推荐的双向绑定实现方式,下面就跟随小编一起深入了解一下defineModel的使用吧
    2024-02-02
  • vue-cli使用stimulsoft.reports.js的详细教程

    vue-cli使用stimulsoft.reports.js的详细教程

    Stimulsoft Reports.JS是一个使用JavaScript和HTML5生成报表的平台。它拥有所有拥来设计,编辑和查看报表的必需组件。该报表工具根据开发人员数量授权而不是根据应用程序的用户数量。接下来通过本文给大家介绍vue-cli使用stimulsoft.reports.js的方法,一起看看吧
    2021-12-12
  • vue远程加载sfc组件思路详解

    vue远程加载sfc组件思路详解

    这篇文章主要介绍了vue远程加载sfc组件思路详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Vue3+Vite中不支持require的方式引入本地图片的解决方案

    Vue3+Vite中不支持require的方式引入本地图片的解决方案

    这篇文章主要介绍了Vue3+Vite中不支持require的方式引入本地图片的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • 深入了解vue2与vue3的生命周期对比

    深入了解vue2与vue3的生命周期对比

    这篇文章主要为大家介绍了vue2与vue3的生命周期对比,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12

最新评论