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