移动端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <van-pull-refresh v-model="stateRefreshing" @refresh="getList()">
  3. <van-list
  4. v-model="stateLoading"
  5. :finished="stateFinished"
  6. finished-text="没有更多了"
  7. @load="getList('+1')"
  8. >
  9. <slot name="default"/>
  10. </van-list>
  11. </van-pull-refresh>
  12. </template>
  13. <script>
  14. // 可刷新, 可下拉的vant列表
  15. export default {
  16. name: 'PagedList',
  17. props: {
  18. value: { // 列表 只写
  19. type: Array,
  20. default: function() {
  21. return [];
  22. },
  23. },
  24. pageNum: { // 页码 读写 无监听
  25. type: Number,
  26. default: 1,
  27. },
  28. pageSize: { // 页量 读写 无监听
  29. type: Number,
  30. default: 10,
  31. },
  32. total: { // 读取总数 只写
  33. type: Number,
  34. default: 0,
  35. },
  36. loading: { // 是否加载中 只写
  37. type: Boolean,
  38. default: false,
  39. },
  40. refreshing: { // 是否刷新中 只写
  41. type: Boolean,
  42. default: false,
  43. },
  44. finished: { // 是否完全加载 只写
  45. type: Boolean,
  46. default: false,
  47. },
  48. getWhenCreated: { // 创建后直接获取列表 只读 无监听
  49. type: Boolean,
  50. default: false,
  51. },
  52. getListFunc: { // 获取数据函数 返回Promise 成功参数传递本次请求响应的列表项数和总数 { length|rows|data, total }
  53. type: Function,
  54. default: null,
  55. },
  56. reload: { // 变更此值为true则重新加载数据 监听
  57. type: Boolean,
  58. default: false,
  59. },
  60. },
  61. components: {
  62. },
  63. data() {
  64. return {
  65. stateLoading: false,
  66. stateRefreshing: false,
  67. stateFinished: !this.getWhenCreated,
  68. stateTotal: 0,
  69. queryParams: {
  70. pageNum: this.pageNum,
  71. pageSize: this.pageSize,
  72. },
  73. }
  74. },
  75. created() {
  76. if(this.getWhenCreated)
  77. this.getList();
  78. },
  79. methods: {
  80. getList(target) {
  81. let type = typeof (target);
  82. this.log(type, target);
  83. if(target && this.stateFinished)
  84. {
  85. this.log('nomore');
  86. this.$emit('nomore');
  87. return;
  88. }
  89. if (target === 0) {
  90. this.setupRefreshing(true);
  91. this.setupFinished(true);
  92. this.setupTotal(0);
  93. this.setupPageNum(1);
  94. this.clearList();
  95. }
  96. else if (type === 'number')
  97. this.setupPageNum(target);
  98. else if (type === 'string') {
  99. this.setupPageNum(eval(this.queryParams.pageNum + target));
  100. }
  101. else
  102. {
  103. this.setupRefreshing(true);
  104. this.setupFinished(true);
  105. this.setupTotal(0);
  106. this.setupPageNum(1);
  107. this.clearList();
  108. }
  109. this.getListFunc(this.queryParams).then((result) => {
  110. if(result.hasOwnProperty('rows'))
  111. {
  112. this.setupTotal(this.stateTotal + result.rows.length);
  113. this.setupFinished(this.stateTotal >= result.total);
  114. }
  115. else if(result.hasOwnProperty('data'))
  116. {
  117. this.setupTotal(this.stateTotal + result.data.length);
  118. this.setupFinished(true);
  119. }
  120. else if(result.hasOwnProperty('length'))
  121. {
  122. this.setupTotal(this.stateTotal + result.length);
  123. this.setupFinished(true);
  124. }
  125. if(result.hasOwnProperty('total'))
  126. {
  127. this.setupFinished(this.stateTotal >= result.total);
  128. }
  129. else
  130. {
  131. this.setupFinished(true);
  132. }
  133. }).catch((err) => {
  134. this.setupError(err);
  135. }).finally(() => {
  136. this.setupLoading(false);
  137. this.setupRefreshing(false);
  138. });
  139. },
  140. clearList() {
  141. this.log(`reload`);
  142. this.$emit('reload');
  143. this.$emit('input', []);
  144. },
  145. setupPageNum(num) {
  146. this.queryParams.pageNum = num;
  147. if(this.queryParams.pageNum != this.pageNum)
  148. this.$emit('update:pageNum', this.queryParams.pageNum);
  149. },
  150. setupTotal(num) {
  151. this.log(`total -> ${num}`);
  152. this.stateTotal = num;
  153. if(this.stateTotal != this.total)
  154. this.$emit('update:total', this.stateTotal);
  155. },
  156. setupLoading(ok) {
  157. this.log(`loading -> ${ok}`);
  158. this.stateLoading = ok;
  159. if(this.stateLoading != this.loading)
  160. this.$emit('update:loading', this.stateLoading);
  161. },
  162. setupFinished(ok) {
  163. this.log(`finished -> ${ok}`);
  164. this.stateFinished = ok;
  165. if(this.stateFinished != this.finished)
  166. this.$emit('update:finished', this.stateFinished);
  167. if(ok)
  168. this.$emit('finished', this.stateTotal);
  169. },
  170. setupError(err) {
  171. this.log(`error -> ${err}`);
  172. this.stateFinished = true;
  173. if(this.stateFinished != this.finished)
  174. this.$emit('update:finished', this.stateFinished);
  175. this.$emit('error', err);
  176. },
  177. setupRefreshing(ok) {
  178. this.log(`refreshing -> ${ok}`);
  179. this.stateRefreshing = ok;
  180. if(this.stateRefreshing != this.refreshing)
  181. this.$emit('update:refreshing', this.stateRefreshing);
  182. },
  183. log() {
  184. return;
  185. console.log('[PagedList]: ', ...arguments);
  186. },
  187. },
  188. watch: {
  189. reload(newVal) {
  190. if(newVal)
  191. {
  192. this.$emit('update:reload', false);
  193. this.getList();
  194. }
  195. },
  196. },
  197. }
  198. </script>
  199. <style scoped lang="scss">
  200. </style>