custom-scroll-list-chengji.vue 4.9 KB

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