custom-scroll-list.vue 4.3 KB

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