当前位置:   article > 正文

H5 vue 移动端 下拉刷新加载数据 (对象数组合并去重 )_h5 下拉加载

h5 下拉加载

npm install vue-infinite-scroll --save

2. 在main.js 引入

import infiniteScroll from 'vue-infinite-scroll'
Vue.use(infiniteScroll)

  1. <template>
  2. <div id="app">
  3. <div class="authorization_box" v-infinite-scroll="loadMore" infinite-scroll-disabled="loading"
  4. infinite-scroll-distance="10">
  5. <!-- 循环数据列表 -->
  6. <div class="list" v-for="(item,index) in datalist" :key="index">
  7. <div>{{item}}</div>
  8. </div>
  9. <!-- 展示“正在加载、已无数据、加载失败” -->
  10. <div class="l-load">{{ loadTxt[loadKey] }}</div>
  11. </div>
  12. </div>
  13. </template>
  14. <script type="text/javascript">
  15. export default{
  16. name: 'App4',
  17. components: {
  18. },
  19. data() {
  20. return {
  21. page: 1,//当前页
  22. total: 0,//总数据数量
  23. pageSize: 10,//每页几个
  24. loadTxt: {
  25. more: "正在加载...",
  26. none: "没有更多了~",
  27. err: "加载失败~"
  28. },
  29. loadKey: "none",
  30. datalist: [],//数据列表
  31. loading: false,
  32. dataMax:0
  33. };
  34. },
  35. mounted() {
  36. this.getData();//一进入页面就调用获取数据的方法
  37. },
  38. methods: {
  39. getData() {
  40. console.log(this.queryParams, 'queryParams+++')
  41. this.queryParams.pageNum = this.page
  42. this.queryParams.pageSize = this.pageSize
  43. getListbr(this.queryParams, this.token).then(res => {
  44. const Arr = []
  45. if (res.status === 200 && res.data.list.length > 0) {
  46. this.nodata = false
  47. this.total = res.data.total
  48. console.log(this.total, 'this.total111')
  49. // this.loading = true
  50. this.loadKey = 'more'
  51. res.data.list.forEach(item => {
  52. Arr.push({
  53. time: item.startTime.substring(5, 11),
  54. starTime: item.startTime.substring(11),
  55. endTime: item.endTime.substring(11),
  56. totalSleepTime: formatSeconds(item.totalSleepTime),
  57. sbdNumbers: item.sbdNumbers,
  58. heartbeatRate: item.heartbeatRate,
  59. id: item.id
  60. })
  61. console.log(JSON.stringify(this.dataList), '****new dataList')
  62. this.dataListNew = Arr
  63. //这里要把每次差的每页的数据 放到一个数组 这里用的是数组合并去重
  64. const newArr = this.dataListNew.concat(Arr)
  65. for (const item1 of newArr) { // 循环json数组对象的内容
  66. let flag = true // 建立标记,判断数据是否重复,true为不重复
  67. for (const item2 of this.dataList) { // 循环新数组的内容
  68. if (item1.id === item2.id) { // 让json数组对象的内容与新数组的内容作比较,相同的话,改变标记为false
  69. flag = false
  70. }
  71. }
  72. if (flag) { // 判断是否重复
  73. this.dataList.push(item1) // 不重复的放入新数组。 新数组的内容会继续进行上边的循环。
  74. }
  75. }
  76. })
  77. console.log(this.dataList, 'datalist')
  78. if (this.total <= this.dataList.length) { // 这里我假设数据加载完了
  79. this.loadKey = 'none'
  80. this.loading = true
  81. }
  82. } else {
  83. this.nodata = true
  84. }
  85. })
  86. },
  87. loadMore() {
  88. console.log('进入loadMore 要在手机上才有效果')
  89. const page = this.page
  90. const size = this.pageSize
  91. const total = this.total
  92. const length = this.dataList.length
  93. const num = page * size
  94. console.log(this.total, 'this.total')
  95. console.log(this.dataList, 'datalist')
  96. console.log(this.dataList.length, 'this.datalist.length')
  97. if (num < total && num === length) {
  98. console.log('loadMore......1111')
  99. ++this.page
  100. console.log(this.page, '多少页++++++?')
  101. this.getData()
  102. }
  103. },
  104. }
  105. }
  106. </script>
  107. <style media="screen">
  108. .authorization_box {
  109. width: 100%;
  110. background-color: #f6f6f6;
  111. padding: 0.5rem 1rem 1rem;
  112. }
  113. .list{
  114. width: 100%;
  115. height: 2.5rem;
  116. border: 1px solid #ccc;
  117. margin-bottom: 0.5rem;
  118. display: flex;
  119. justify-content: center;
  120. align-items: center;
  121. }
  122. .l-load {
  123. text-align: center;
  124. font-size: 0.625rem;
  125. color: #aaa;
  126. margin-top: 1rem;
  127. }
  128. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/106250
推荐阅读
相关标签
  

闽ICP备14008679号