yf-more-list.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <view :id="id">
  3. <uni-list>
  4. <template v-for="(item,index) in dataList">
  5. <slot name="item" v-bind:data="item"></slot>
  6. </template>
  7. </uni-list>
  8. <yf-empty-view :isShow="more==='noMore' && query.current==1 && dataList.length==0"></yf-empty-view>
  9. <uni-load-more v-if="dataList.length > 0 " :status="more" :contentText="contentText"></uni-load-more>
  10. </view>
  11. </template>
  12. <script>
  13. import request from '@/common/request.js'
  14. export default {
  15. name: 'yf-more-list',
  16. components: {},
  17. props: {
  18. url: String, // 页面地址
  19. params: Object
  20. },
  21. data() {
  22. return {
  23. id: new Date().getTime(),
  24. contentText: {
  25. contentdown: "上拉显示更多",
  26. contentrefresh: "正在加载...",
  27. contentnomore: "没有更多数据了..."
  28. },
  29. loading: false,
  30. more: 'more',
  31. query: {
  32. current: 1,
  33. size: 10,
  34. params: {
  35. }
  36. },
  37. dataList: []
  38. }
  39. },
  40. watch: {
  41. params: {
  42. handler(val) {
  43. this.query.params = val
  44. this.initData()
  45. },
  46. deep: true
  47. }
  48. },
  49. mounted() {
  50. this.id = new Date().getTime()
  51. this.query.params = this.params
  52. this.loadData()
  53. },
  54. methods: {
  55. initData() {
  56. this.more = 'more'
  57. this.query.current = 1
  58. this.dataList = []
  59. this.loadData();
  60. },
  61. loadData() {
  62. // 避免重复加载
  63. if (this.more === 'loading' || this.more == 'noMore') {
  64. return;
  65. }
  66. this.more = 'loading'
  67. // 加载数据
  68. request.post(this.url, this.query).then(res => {
  69. // 清空列表
  70. if (this.query.current === 1) {
  71. this.dataList = []
  72. }
  73. // 追加数据
  74. this.dataList = this.dataList.concat(res.records)
  75. console.log(this.dataList)
  76. if (this.query.current >= res.pages) {
  77. this.more = 'noMore'
  78. } else {
  79. this.more = 'more'
  80. this.query.current += 1
  81. }
  82. })
  83. }
  84. }
  85. }
  86. </script>
  87. <style>
  88. </style>