custom-scroll-list-chengji.vue 4.9 KB

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