tui-load-list.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="page-load-list">
  3. <slot></slot>
  4. <block v-if="apiUrl">
  5. <!-- <tui-loadmore v-if="loadState=='loading'" :index="2"></tui-loadmore> -->
  6. <block v-if="!onlyFirstPage">
  7. <tui-divider v-if="loadState=='over'&&list&&list.length>0" width="80%" :backgroundColor="backColor">
  8. 没有更多了
  9. </tui-divider>
  10. </block>
  11. <view class="tui-no-data xd" v-if="loadState=='over'&&list.length<=0">
  12. <tui-no-data class="tui-no-data" :positiontype="1" :fixed="false"
  13. imgUrl="/static/images/toast/img_nodata.png" imgWidth="120" imgHeight="120">暂无数据
  14. </tui-no-data>
  15. </view>
  16. <block v-if="!onlyFirstPage">
  17. <tui-divider v-if="loadState!='over'&&loadState!='loading'" width="80%"
  18. :backgroundColor="backColor">上拉加载数据
  19. </tui-divider>
  20. </block>
  21. </block>
  22. </view>
  23. </template>
  24. <script>
  25. import request from '@/utils/request'
  26. export default {
  27. data() {
  28. return {
  29. loadState: "",
  30. page: 0,
  31. total: 0,
  32. list: [],
  33. pTime: 0
  34. };
  35. },
  36. props: {
  37. value: {
  38. type: Array,
  39. default: () => {
  40. return []
  41. }
  42. },
  43. onlyFirstPage: {
  44. type: Boolean,
  45. default: false
  46. },
  47. datatype: {
  48. type: String,
  49. default: ""
  50. },
  51. backColor: {
  52. type: String,
  53. default: "#f4f4f4"
  54. },
  55. apiUrl: {
  56. type: String,
  57. default: ""
  58. },
  59. limit: {
  60. type: Number,
  61. default: 20
  62. },
  63. postData: {
  64. type: Object,
  65. default: null
  66. }
  67. },
  68. watch: {
  69. // rushData(val) {
  70. // if (!this.datatype) {
  71. // return false
  72. // }
  73. // const rushdata = {
  74. // "name": "list",
  75. // "data": String(this.datatype)
  76. // }
  77. // const isrush = this.$store.commit("delRushData", rushdata)
  78. // if (isrush) {
  79. // this.$nextTick(function() {
  80. // this.loadData("rush")
  81. // })
  82. // }
  83. // },
  84. apiUrl() {
  85. this.loadData("rush")
  86. },
  87. // postData: {
  88. // handler: function() {
  89. // this.loadData("rush")
  90. // },
  91. // // 开启深度监听:只要obj中的任何一个属性发生改变,都会触发相应的代码
  92. // deep: true
  93. // }
  94. },
  95. created() {
  96. // console.log('this.apiUrl',this.apiUrl)
  97. this.list = this.value
  98. this.loadData("rush")
  99. // if (!this.datatype) {
  100. // return false
  101. // }
  102. // const rushdata = {
  103. // "name": "list",
  104. // "data": String(this.datatype)
  105. // }
  106. // this.$store.dispatch("delRushData", rushdata)
  107. },
  108. methods: {
  109. loadData(type = "") {
  110. if (!this.apiUrl) {
  111. return false
  112. }
  113. if (type == "rush") {
  114. this.loadState = ''
  115. this.list = []
  116. this.page = 0
  117. this.$emit("input", this.list)
  118. } else if (this.onlyFirstPage) {
  119. return false
  120. }
  121. if (this.loadState == "loading") {
  122. return false
  123. }
  124. if (this.loadState == "over") {
  125. return false
  126. }
  127. this.loadState = "loading"
  128. this.page++;
  129. const data = Object.assign({}, this.postData, {
  130. 'pageNum': this.page,
  131. 'pageSize': this.limit
  132. })
  133. request({
  134. 'url': this.apiUrl,
  135. 'method': 'get',
  136. params: data
  137. }).then(res => {
  138. if ('data' in res && res.data) {
  139. this.list = this.list.concat(res.data)
  140. } else if ('rows' in res && res.rows) {
  141. this.list = this.list.concat(res.rows)
  142. }
  143. this.total = res.total
  144. if (this.list.length >= this.total) {
  145. this.loadState = "over"
  146. } else if (this.onlyFirstPage) {
  147. this.loadState = "over"
  148. } else {
  149. this.loadState = ""
  150. }
  151. this.$emit("input", this.list)
  152. this.$emit("change", {
  153. total: this.total,
  154. page: this.page,
  155. limit: this.limit,
  156. list: this.list
  157. })
  158. }).catch(res => {})
  159. }
  160. }
  161. }
  162. </script>
  163. <style lang="scss" scoped>
  164. .page-load-list {
  165. overflow: hidden;
  166. .tui-no-data {
  167. width: 100%;
  168. height: 320rpx;
  169. display: flex;
  170. justify-content: center;
  171. }
  172. }
  173. </style>