list.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <view>
  3. <u-empty v-if="!list || list.length == 0" marginTop="100"/>
  4. <block v-else>
  5. <u-swipe-action v-if="edit || del" ref="swipes">
  6. <u-swipe-action-item v-for="(item,i) in list" :key="item.id" :options="swipes" @click="clickSwipe($event,item,i)">
  7. <view :class="border ? 'border-b-i-m' + (i==0?' border-t-i-m':'') : ''">
  8. <slot :item="item"/>
  9. </view>
  10. </u-swipe-action-item>
  11. </u-swipe-action>
  12. <view v-else v-for="(item,i) in list" :key="i" :class="border ? 'border-b-i-m' + (i==0?' border-t-i-m':'') : ''">
  13. <slot :item="item"/>
  14. </view>
  15. <view class="p-10"><u-loadmore :status="page.status" line/></view>
  16. <u-back-top :scroll-top="scrollTop" bottom="30" duration="300"/>
  17. </block>
  18. </view>
  19. </template>
  20. <script>
  21. import {bizConst} from '@/api/biz/biz.js'
  22. export default {
  23. props: {
  24. func: String,
  25. params: {
  26. type: Object,
  27. default() {
  28. return {}
  29. },
  30. },
  31. pageSize: {
  32. type: Number,
  33. default: bizConst.pageSize,
  34. },
  35. border: {//是否显示中间的分隔线
  36. type: Boolean,
  37. default: false,
  38. },
  39. edit: {//是否可以划出编辑按钮
  40. type: Boolean,
  41. default: false,
  42. },
  43. del: {//是否可以划出删除按钮
  44. type: Boolean,
  45. default: false,
  46. },
  47. },
  48. computed: {
  49. swipes() {
  50. let swipes = []
  51. if (this.edit) {
  52. swipes.push({
  53. text:'编辑',
  54. style: {
  55. backgroundColor:'#4F8477',
  56. },
  57. })
  58. }
  59. if (this.del) {
  60. swipes.push({
  61. text:'删除',
  62. style: {
  63. backgroundColor:'#DC545A',
  64. },
  65. })
  66. }
  67. console.log(swipes)
  68. return swipes
  69. },
  70. },
  71. watch: {
  72. params: {
  73. handler: function(newValue) {
  74. console.log('watch:params=' + newValue)
  75. this.refresh()
  76. },
  77. deep: true,
  78. },
  79. },
  80. data() {
  81. return {
  82. list: [],
  83. page: {
  84. current: 0,
  85. status: 'loadmore',//loading,nomore
  86. },
  87. scrollTop: 0, // 滚动条位置,距顶为0
  88. }
  89. },
  90. methods: {
  91. refresh(params) {//刷新
  92. this.getDatas(false, params)
  93. },
  94. getDatas(isNextPage, params) {//翻页
  95. if (this.page.status == 'loading') {//如果在加载过程中再次请求,那么不处理
  96. return
  97. }
  98. if (!isNextPage) {//重新刷新
  99. this.list = []
  100. this.page.current = 0
  101. } else if (this.page.status == 'nomore') {//请求下一页时,如果没数据了,直接退出
  102. return
  103. }
  104. this.params = params || this.params
  105. this.page.status = 'loading'
  106. this.$u.api[this.func].call(this, this.page.current + 1, this.pageSize, this.params).then(res => {
  107. this.list = this.list.concat(res.data.records)
  108. this.page.current++ //加一页
  109. this.page.status = this.page.current < res.data.pages ? 'loadmore' : 'nomore'
  110. console.log('加载完第' + this.page.current + '页')
  111. })
  112. },
  113. clickSwipe(e, item, index) {
  114. if (e.index == 0) {//编辑
  115. this.$emit("toEdit", item, index)
  116. } else{//删除
  117. this.$emit("toDel", item, index)
  118. }
  119. this.$refs.swipes.closeOther()//直接调组件的未公布的方法
  120. },
  121. delOne(index) {//删除前端数据
  122. this.list.splice(index, 1)
  123. },
  124. scroll(scrollTop) {
  125. this.scrollTop = scrollTop
  126. },
  127. reachBottom() {
  128. this.getDatas(true)
  129. },
  130. },
  131. }
  132. </script>
  133. <style>
  134. </style>