pull-down-refresh.uvue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1;">
  4. <!-- #endif -->
  5. <!-- 实际开发中,长列表应该使用list-view -->
  6. <view class="uni-padding-wrap uni-common-mt">
  7. <text class="text" v-for="(num,index) in data" :key="index">list - {{num}}</text>
  8. <view v-if="showLoadMore">{{loadMoreText}}</view>
  9. </view>
  10. <!-- #ifdef APP -->
  11. </scroll-view>
  12. <!-- #endif -->
  13. </template>
  14. <script lang="uts">
  15. export default {
  16. data() {
  17. return {
  18. data: [] as Array<number>,
  19. loadMoreText: "加载中...",
  20. showLoadMore: false,
  21. max: 0,
  22. pulldownRefreshTriggered: false
  23. }
  24. },
  25. onReady() {
  26. uni.startPullDownRefresh();
  27. this.initData();
  28. },
  29. onReachBottom() {
  30. console.log("onReachBottom");
  31. if (this.max > 40) {
  32. this.loadMoreText = "没有更多数据了!"
  33. return;
  34. }
  35. this.showLoadMore = true;
  36. setTimeout(() => {
  37. this.setListData();
  38. }, 300);
  39. },
  40. onPullDownRefresh() {
  41. console.log('onPullDownRefresh');
  42. this.pulldownRefreshTriggered = true
  43. this.initData();
  44. },
  45. methods: {
  46. initData() {
  47. setTimeout(() => {
  48. this.max = 0;
  49. this.data = [];
  50. let data : Array<number> = [];
  51. this.max += 20;
  52. for (let i : number = this.max - 19; i < this.max + 1; i++) {
  53. data.push(i)
  54. }
  55. this.data = this.data.concat(data);
  56. uni.stopPullDownRefresh();
  57. }, 1000);
  58. },
  59. setListData() {
  60. let data : Array<number> = [];
  61. this.max += 10;
  62. for (let i : number = this.max - 9; i < this.max + 1; i++) {
  63. data.push(i)
  64. }
  65. this.data = this.data.concat(data);
  66. }
  67. }
  68. }
  69. </script>
  70. <style>
  71. .text {
  72. margin: 6px 0;
  73. width: 100%;
  74. background-color: #fff;
  75. height: 52px;
  76. line-height: 52px;
  77. text-align: center;
  78. color: #555;
  79. border-radius: 4px;
  80. }
  81. </style>