custom-scroll-list-chengji.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. watch,
  24. onMounted,
  25. computed,
  26. } from "vue";
  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 Emits = defineEmits(['tabChange']);
  78. const page = ref(1);
  79. const list = ref([]); // 项目列表
  80. const triggered = ref(false); // 是否触发下拉刷新
  81. const freshing = ref(false); // 是否加载中
  82. const total = ref(0); // 项目总数
  83. const name = ref(''); // 查询名
  84. const activeTab = ref(props.defaultTab);
  85. const status = ref('more');
  86. const contentText = {
  87. contentdown: '查看更多',
  88. contentrefresh: '加载中',
  89. contentnomore: '没有更多'
  90. }
  91. const currentRefreshFn = ref(props.refreshFn);
  92. watch(
  93. () => props.refreshFn,
  94. (newFn) => {
  95. currentRefreshFn.value = newFn;
  96. reset();
  97. getData("do-search");
  98. }
  99. );
  100. /**
  101. * 是否已完全加载
  102. */
  103. const isComplete = computed(() => {
  104. if (total.value === 0) {
  105. return false;
  106. }
  107. return total.value === list.value.length
  108. })
  109. // 重置
  110. function reset() {
  111. list.value = [];
  112. page.value = 1;
  113. triggered.value = false;
  114. freshing.value = false;
  115. total.value = 0;
  116. status.value = 'more';
  117. }
  118. // 切换tab
  119. function onTavChange(item) {
  120. activeTab.value = item.value;
  121. name.value = "";
  122. reset();
  123. Emits('tabChange', item)
  124. // getData("do-search");
  125. }
  126. function tabChangeSearch(item){
  127. activeTab.value = item.value;
  128. name.value = "";
  129. props.refreshFn = item.refreshFn
  130. reset();
  131. getData("do-search");
  132. }
  133. // 查询
  134. function onSearch({
  135. value
  136. }) {
  137. name.value = value;
  138. reset();
  139. getData("do-search");
  140. }
  141. // 获取数据
  142. function getData(action) {
  143. const options = Object.assign({}, {
  144. page: page.value,
  145. size: props.size,
  146. });
  147. if (props.hasTab) {
  148. options[props.tabKey] = activeTab.value;
  149. }
  150. if (props.hasSearcBar) {
  151. options[props.searchBarKey] = name.value;
  152. }
  153. currentRefreshFn.value(options).then(res => {
  154. total.value = res.data.total;
  155. action === "do-search" && (list.value = res.data.data); // 查询更新
  156. action === "pull-down-refresh" && (list.value = res.data.data); // 下拉更新数据
  157. action === "reach-buttom" && (list.value = [...list.value, ...res.data.data]); // 无限滚动更新数据
  158. }).finally(() => {
  159. triggered.value = false;
  160. freshing.value = false;
  161. if (total.value !== list.value.length) {
  162. status.value = 'more';
  163. } else {
  164. status.value = 'noMore';
  165. }
  166. })
  167. }
  168. onLoad(() => {
  169. freshing.value = false;
  170. setTimeout(() => {
  171. triggered.value = true
  172. }, 50)
  173. })
  174. // 下拉刷新触发
  175. function onRefresh() {
  176. if (freshing.value) return;
  177. status.value = 'loading';
  178. freshing.value = true;
  179. triggered.value = true;
  180. page.value = 1;
  181. getData('pull-down-refresh');
  182. }
  183. // 下拉刷新复位
  184. function onRestore() {
  185. triggered.value = 'restore'; // 需要重置
  186. }
  187. // 无限滚动
  188. function onReachBottom() {
  189. if (freshing.value) return;
  190. if (isComplete.value) return;
  191. freshing.value = true;
  192. page.value++;
  193. getData('reach-buttom')
  194. }
  195. defineExpose({
  196. onRefresh,
  197. tabChangeSearch
  198. })
  199. </script>
  200. <style lang="scss">
  201. .scroll-container {
  202. height: calc(100vh - 220rpx)
  203. }
  204. </style>