z-paging-swiper-item.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!-- z-paging -->
  2. <!-- github地址:https://github.com/SmileZXLee/uni-z-paging -->
  3. <!-- dcloud地址:https://ext.dcloud.net.cn/plugin?id=3935 -->
  4. <!-- 反馈QQ群:790460711 -->
  5. <!-- 滑动切换选项卡swiper-item,此组件支持easycom规范,可以在项目中直接引用 -->
  6. <template>
  7. <view class="zp-swiper-item-container">
  8. <z-paging ref="paging" :fixed="false"
  9. :auto="false" :useVirtualList="useVirtualList" :useInnerList="useInnerList" :cellKeyName="cellKeyName" :innerListStyle="innerListStyle"
  10. :preloadPage="preloadPage" :cellHeightMode="cellHeightMode" :virtualScrollFps="virtualScrollFps" :virtualListCol="virtualListCol"
  11. @query="_queryList" @listChange="_updateList" style="height: 100%;">
  12. <slot />
  13. <template #header>
  14. <slot name="header"/>
  15. </template>
  16. <template #cell="{item,index}">
  17. <slot name="cell" :item="item" :index="index"/>
  18. </template>
  19. <template #footer>
  20. <slot name="footer"/>
  21. </template>
  22. </z-paging>
  23. </view>
  24. </template>
  25. <script>
  26. import zPaging from '../z-paging/z-paging'
  27. export default {
  28. name: "z-paging-swiper-item",
  29. components: { zPaging },
  30. data() {
  31. return {
  32. firstLoaded: false
  33. }
  34. },
  35. props: {
  36. // 当前组件的index,也就是当前组件是swiper中的第几个
  37. tabIndex: {
  38. type: Number,
  39. default: function() {
  40. return 0
  41. }
  42. },
  43. // 当前swiper切换到第几个index
  44. currentIndex: {
  45. type: Number,
  46. default: function() {
  47. return 0
  48. }
  49. },
  50. // 是否使用虚拟列表,默认为否
  51. useVirtualList: {
  52. type: Boolean,
  53. default: false
  54. },
  55. // 是否在z-paging内部循环渲染列表(内置列表),默认为否。若use-virtual-list为true,则此项恒为true
  56. useInnerList: {
  57. type: Boolean,
  58. default: false
  59. },
  60. // 内置列表cell的key名称,仅nvue有效,在nvue中开启use-inner-list时必须填此项
  61. cellKeyName: {
  62. type: String,
  63. default: ''
  64. },
  65. // innerList样式
  66. innerListStyle: {
  67. type: Object,
  68. default: function() {
  69. return {};
  70. }
  71. },
  72. // 预加载的列表可视范围(列表高度)页数,默认为12,即预加载当前页及上下各12页的cell。此数值越大,则虚拟列表中加载的dom越多,内存消耗越大(会维持在一个稳定值),但增加预加载页面数量可缓解快速滚动短暂白屏问题
  73. preloadPage: {
  74. type: [Number, String],
  75. default: 12
  76. },
  77. // 虚拟列表cell高度模式,默认为fixed,也就是每个cell高度完全相同,将以第一个cell高度为准进行计算。可选值【dynamic】,即代表高度是动态非固定的,【dynamic】性能低于【fixed】。
  78. cellHeightMode: {
  79. type: String,
  80. default: 'fixed'
  81. },
  82. // 虚拟列表列数,默认为1。常用于每行有多列的情况,例如每行有2列数据,需要将此值设置为2
  83. virtualListCol: {
  84. type: [Number, String],
  85. default: 1
  86. },
  87. // 虚拟列表scroll取样帧率,默认为60,过高可能出现卡顿等问题
  88. virtualScrollFps: {
  89. type: [Number, String],
  90. default: 60
  91. },
  92. },
  93. watch: {
  94. currentIndex: {
  95. handler(newVal, oldVal) {
  96. if (newVal === this.tabIndex) {
  97. // 懒加载,当滑动到当前的item时,才去加载
  98. if (!this.firstLoaded) {
  99. this.$nextTick(()=>{
  100. let delay = 5;
  101. // #ifdef MP-TOUTIAO
  102. delay = 100;
  103. // #endif
  104. setTimeout(() => {
  105. this.$refs.paging.reload().catch(() => {});
  106. }, delay);
  107. })
  108. }
  109. }
  110. },
  111. immediate: true
  112. }
  113. },
  114. methods: {
  115. reload(data) {
  116. return this.$refs.paging.reload(data);
  117. },
  118. complete(data) {
  119. this.firstLoaded = true;
  120. return this.$refs.paging.complete(data);
  121. },
  122. _queryList(pageNo, pageSize, from) {
  123. this.$emit('query', pageNo, pageSize, from);
  124. },
  125. _updateList(list) {
  126. this.$emit('updateList', list);
  127. }
  128. }
  129. }
  130. </script>
  131. <style scoped>
  132. .zp-swiper-item-container {
  133. /* #ifndef APP-NVUE */
  134. height: 100%;
  135. /* #endif */
  136. /* #ifdef APP-NVUE */
  137. flex: 1;
  138. /* #endif */
  139. }
  140. </style>