z-paging-swiper-item.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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">
  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. /**
  28. * z-paging-swiper-item 组件
  29. * @description swiper+list极简写法中使用到,实际上就是对普通的swiper+list中swiper-item的包装封装,用以简化写法,但个性化配置局限较多
  30. * @tutorial https://z-paging.zxlee.cn/api/sub-components/main.html#z-paging-swiper-item配置
  31. * @notice 以下为 z-paging-swiper-item 的配置项
  32. * @property {Number} tabIndex 当前组件的 index,也就是当前组件是 swiper 中的第几个,默认为 0
  33. * @property {Number} currentIndex 当前 swiper 切换到第几个 index,默认为 0
  34. * @property {Boolean} useVirtualList 是否使用虚拟列表,默认为 false
  35. * @property {Boolean} useInnerList 是否在 z-paging 内部循环渲染列表(内置列表),默认为 false。若 useVirtualList 为 true,则此项恒为 true
  36. * @property {String} cellKeyName 内置列表 cell 的 key 名称,仅 nvue 有效,在 nvue 中开启 useInnerList 时必须填此项,默认为 ''
  37. * @property {Object} innerListStyle innerList 样式,默认为 {}
  38. * @property {Number|String} preloadPage 预加载的列表可视范围(列表高度)页数,默认为 12。此数值越大,则虚拟列表中加载的 dom 越多,内存消耗越大(会维持在一个稳定值),但增加预加载页面数量可缓解快速滚动短暂白屏问题
  39. * @property {String} cellHeightMode 虚拟列表 cell 高度模式,默认为 'fixed',也就是每个 cell 高度完全相同,将以第一个 cell 高度为准进行计算。可选值【dynamic】,即代表高度是动态非固定的,【dynamic】性能低于【fixed】
  40. * @property {Number|String} virtualListCol 虚拟列表列数,默认为 1。常用于每行有多列的情况,例如每行有 2 列数据,需要将此值设置为 2
  41. * @property {Number|String} virtualScrollFps 虚拟列表 scroll 取样帧率,默认为 60,过高可能出现卡顿等问题
  42. * @example <z-paging-swiper-item ref="swiperItem" :tabIndex="index" :currentIndex="current" @query="queryList" @updateList="updateList"></z-paging-swiper-item>
  43. */
  44. export default {
  45. name: "z-paging-swiper-item",
  46. components: { zPaging },
  47. data() {
  48. return {
  49. firstLoaded: false
  50. }
  51. },
  52. props: {
  53. // 当前组件的index,也就是当前组件是swiper中的第几个
  54. tabIndex: {
  55. type: Number,
  56. default: function() {
  57. return 0
  58. }
  59. },
  60. // 当前swiper切换到第几个index
  61. currentIndex: {
  62. type: Number,
  63. default: function() {
  64. return 0
  65. }
  66. },
  67. // 是否使用虚拟列表,默认为否
  68. useVirtualList: {
  69. type: Boolean,
  70. default: false
  71. },
  72. // 是否在z-paging内部循环渲染列表(内置列表),默认为否。若use-virtual-list为true,则此项恒为true
  73. useInnerList: {
  74. type: Boolean,
  75. default: false
  76. },
  77. // 内置列表cell的key名称,仅nvue有效,在nvue中开启use-inner-list时必须填此项
  78. cellKeyName: {
  79. type: String,
  80. default: ''
  81. },
  82. // innerList样式
  83. innerListStyle: {
  84. type: Object,
  85. default: function() {
  86. return {};
  87. }
  88. },
  89. // 预加载的列表可视范围(列表高度)页数,默认为12,即预加载当前页及上下各12页的cell。此数值越大,则虚拟列表中加载的dom越多,内存消耗越大(会维持在一个稳定值),但增加预加载页面数量可缓解快速滚动短暂白屏问题
  90. preloadPage: {
  91. type: [Number, String],
  92. default: 12
  93. },
  94. // 虚拟列表cell高度模式,默认为fixed,也就是每个cell高度完全相同,将以第一个cell高度为准进行计算。可选值【dynamic】,即代表高度是动态非固定的,【dynamic】性能低于【fixed】。
  95. cellHeightMode: {
  96. type: String,
  97. default: 'fixed'
  98. },
  99. // 虚拟列表列数,默认为1。常用于每行有多列的情况,例如每行有2列数据,需要将此值设置为2
  100. virtualListCol: {
  101. type: [Number, String],
  102. default: 1
  103. },
  104. // 虚拟列表scroll取样帧率,默认为60,过高可能出现卡顿等问题
  105. virtualScrollFps: {
  106. type: [Number, String],
  107. default: 60
  108. },
  109. },
  110. watch: {
  111. currentIndex: {
  112. handler(newVal, oldVal) {
  113. if (newVal === this.tabIndex) {
  114. // 懒加载,当滑动到当前的item时,才去加载
  115. if (!this.firstLoaded) {
  116. this.$nextTick(()=>{
  117. let delay = 5;
  118. // #ifdef MP-TOUTIAO
  119. delay = 100;
  120. // #endif
  121. setTimeout(() => {
  122. this.$refs.paging.reload().catch(() => {});
  123. }, delay);
  124. })
  125. }
  126. }
  127. },
  128. immediate: true
  129. }
  130. },
  131. methods: {
  132. reload(data) {
  133. return this.$refs.paging.reload(data);
  134. },
  135. complete(data) {
  136. this.firstLoaded = true;
  137. return this.$refs.paging.complete(data);
  138. },
  139. _queryList(pageNo, pageSize, from) {
  140. this.$emit('query', pageNo, pageSize, from);
  141. },
  142. _updateList(list) {
  143. this.$emit('updateList', list);
  144. }
  145. }
  146. }
  147. </script>
  148. <style scoped>
  149. .zp-swiper-item-container {
  150. /* #ifndef APP-NVUE */
  151. height: 100%;
  152. /* #endif */
  153. /* #ifdef APP-NVUE */
  154. flex: 1;
  155. /* #endif */
  156. }
  157. </style>