custom-scroll-list.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <!-- 查询区域 -->
  3. <uni-search-bar class="mobile-scroll-search" v-model="name" radius="100" v-if="hasSearcBar" :placeholder="placeholder"
  4. :bgColor="searchBarColor" clearButton="auto" cancelButton="none" @confirm="onSearch" @blur="onSearch" />
  5. <!-- tab选择区域 -->
  6. <view class="lli-status-box" v-if="hasTab"
  7. >
  8. <text :class="['status-item', activeTab === item.value? 'click':'' ]" v-for="item in tabList" :key="item.value"
  9. @click="onTavChange(item)">{{item.label}}</text>
  10. </view>
  11. <!-- 无限滚动区域 -->
  12. <scroll-view :scroll-y="true" :refresher-enabled="true" :refresher-triggered="triggered"
  13. :refresher-threshold="100" refresher-background="#F3F3F4" @refresherrefresh="onRefresh"
  14. @scrolltolower="onReachBottom" @refresherrestore="onRestore"
  15. :style="{ height: `calc(100vh - ${statusBarHeight}px - 264rpx);`}">
  16. <slot :list="list"></slot>
  17. <uni-load-more :status="status" :contentText="contentText"></uni-load-more>
  18. <!-- <view style="width: 100%;text-align: center;font-size: 14px;color:#ccc;" key="a333" v-if="isComplete">没有更多啦~
  19. </view> -->
  20. </scroll-view>
  21. </template>
  22. <script setup>
  23. import {
  24. ref,
  25. onMounted,
  26. computed,
  27. } from "vue";
  28. import {
  29. onLoad
  30. } from "@dcloudio/uni-app"
  31. const statusBarHeight = ref(0);
  32. const props = defineProps({
  33. refreshFn: {
  34. type: Function,
  35. required: true
  36. },
  37. tabData: {
  38. type: Object
  39. },
  40. size: {
  41. type: Number,
  42. default: 5
  43. },
  44. hasSearcBar: {
  45. type: Boolean,
  46. default: true,
  47. },
  48. searchBarKey: {
  49. type: String,
  50. default: 'name'
  51. },
  52. hasTab: {
  53. type: Boolean,
  54. default: true,
  55. },
  56. defaultTab: {
  57. type: [String, Number]
  58. },
  59. tabList: {
  60. type: Array,
  61. default: () => [],
  62. },
  63. tabKey: {
  64. type: String,
  65. default: 'status'
  66. },
  67. placeholder: {
  68. type: String,
  69. default: '请输入考试名称'
  70. },
  71. searchBarColor: {
  72. type: String,
  73. default: "#F3F3F4"
  74. },
  75. tabData: {
  76. type: Object,
  77. }
  78. })
  79. const page = ref(1);
  80. const list = ref([]); // 项目列表
  81. const triggered = ref(false); // 是否触发下拉刷新
  82. const freshing = ref(false); // 是否加载中
  83. const total = ref(0); // 项目总数
  84. const name = ref(''); // 查询名
  85. const activeTab = ref(props.defaultTab);
  86. const status = ref('more');
  87. const contentText = {
  88. contentdown: '查看更多',
  89. contentrefresh: '加载中',
  90. contentnomore: '没有更多'
  91. }
  92. /**
  93. * 是否已完全加载
  94. */
  95. const isComplete = computed(() => {
  96. if (total.value === 0) {
  97. return false;
  98. }
  99. return total.value === list.value.length
  100. })
  101. onMounted(() => {
  102. uni.getSystemInfo({
  103. success: (res) => {
  104. statusBarHeight.value = res.statusBarHeight;
  105. }
  106. });
  107. });
  108. // 重置
  109. function reset() {
  110. list.value = [];
  111. page.value = 1;
  112. triggered.value = false;
  113. freshing.value = false;
  114. total.value = 0;
  115. status.value = 'more';
  116. }
  117. // 切换tab
  118. function onTavChange(item) {
  119. activeTab.value = item.value;
  120. name.value = "";
  121. reset();
  122. getData("do-search");
  123. }
  124. // 查询
  125. function onSearch({
  126. value
  127. }) {
  128. name.value = value;
  129. reset();
  130. getData("do-search");
  131. }
  132. // 获取数据
  133. function getData(action) {
  134. const options = Object.assign({}, {
  135. page: page.value,
  136. size: props.size,
  137. });
  138. if (props.hasTab) {
  139. options[props.tabKey] = activeTab.value;
  140. }
  141. if (props.hasSearcBar) {
  142. options[props.searchBarKey] = name.value;
  143. }
  144. props.refreshFn(options).then(res => {
  145. total.value = res.data.total;
  146. action === "do-search" && (list.value = res.data.data); // 查询更新
  147. action === "pull-down-refresh" && (list.value = res.data.data); // 下拉更新数据
  148. action === "reach-buttom" && (list.value = [...list.value, ...res.data.data]); // 无限滚动更新数据
  149. }).finally(() => {
  150. triggered.value = false;
  151. freshing.value = false;
  152. if (total.value !== list.value.length) {
  153. status.value = 'more';
  154. } else {
  155. status.value = 'noMore';
  156. }
  157. })
  158. }
  159. onLoad(() => {
  160. freshing.value = false;
  161. setTimeout(() => {
  162. triggered.value = true
  163. }, 50)
  164. })
  165. // 下拉刷新触发
  166. function onRefresh() {
  167. if (freshing.value) return;
  168. status.value = 'loading';
  169. freshing.value = true;
  170. triggered.value = true;
  171. page.value = 1;
  172. getData('pull-down-refresh');
  173. }
  174. // 下拉刷新复位
  175. function onRestore() {
  176. triggered.value = 'restore'; // 需要重置
  177. }
  178. // 无限滚动
  179. function onReachBottom() {
  180. if (freshing.value) return;
  181. if (isComplete.value) return;
  182. freshing.value = true;
  183. page.value++;
  184. getData('reach-buttom')
  185. }
  186. defineExpose({
  187. onRefresh
  188. })
  189. </script>