custom-scroll-list.vue 4.4 KB

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