table.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view>
  3. <view class="uni-container">
  4. <uni-table ref="table" :loading="loading" border stripe type="selection" emptyText="暂无更多数据" @selection-change="selectionChange">
  5. <uni-tr>
  6. <uni-th width="150" align="center">日期</uni-th>
  7. <uni-th width="150" align="center">姓名</uni-th>
  8. <uni-th align="center">地址</uni-th>
  9. <uni-th width="204" align="center">设置</uni-th>
  10. </uni-tr>
  11. <uni-tr v-for="(item, index) in tableData" :key="index">
  12. <uni-td>{{ item.date }}</uni-td>
  13. <uni-td>
  14. <view class="name">{{ item.name }}</view>
  15. </uni-td>
  16. <uni-td align="center">{{ item.address }}</uni-td>
  17. <uni-td>
  18. <view class="uni-group">
  19. <button class="uni-button" size="mini" type="primary">修改</button>
  20. <button class="uni-button" size="mini" type="warn">删除</button>
  21. </view>
  22. </uni-td>
  23. </uni-tr>
  24. </uni-table>
  25. <view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize" :current="pageCurrent" :total="total" @change="change" /></view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import tableData from './tableData.js'
  31. export default {
  32. data() {
  33. return {
  34. searchVal: '',
  35. tableData: [],
  36. // 每页数据量
  37. pageSize: 10,
  38. // 当前页
  39. pageCurrent: 1,
  40. // 数据总量
  41. total: 0,
  42. loading: false
  43. }
  44. },
  45. onLoad() {
  46. this.selectedIndexs = []
  47. this.getData(1)
  48. },
  49. methods: {
  50. // 多选处理
  51. selectedItems() {
  52. return this.selectedIndexs.map(i => this.tableData[i])
  53. },
  54. // 多选
  55. selectionChange(e) {
  56. console.log(e.detail.index)
  57. this.selectedIndexs = e.detail.index
  58. },
  59. //批量删除
  60. delTable() {
  61. console.log(this.selectedItems())
  62. },
  63. // 分页触发
  64. change(e) {
  65. this.$refs.table.clearSelection()
  66. this.selectedIndexs.length = 0
  67. this.getData(e.current)
  68. },
  69. // 搜索
  70. search() {
  71. this.getData(1, this.searchVal)
  72. },
  73. // 获取数据
  74. getData(pageCurrent, value = '') {
  75. this.loading = true
  76. this.pageCurrent = pageCurrent
  77. this.request({
  78. pageSize: this.pageSize,
  79. pageCurrent: pageCurrent,
  80. value: value,
  81. success: res => {
  82. // console.log('data', res);
  83. this.tableData = res.data
  84. this.total = res.total
  85. this.loading = false
  86. }
  87. })
  88. },
  89. // 伪request请求
  90. request(options) {
  91. const { pageSize, pageCurrent, success, value } = options
  92. let total = tableData.length
  93. let data = tableData.filter((item, index) => {
  94. const idx = index - (pageCurrent - 1) * pageSize
  95. return idx < pageSize && idx >= 0
  96. })
  97. if (value) {
  98. data = []
  99. tableData.forEach(item => {
  100. if (item.name.indexOf(value) !== -1) {
  101. data.push(item)
  102. }
  103. })
  104. total = data.length
  105. }
  106. setTimeout(() => {
  107. typeof success === 'function' &&
  108. success({
  109. data: data,
  110. total: total
  111. })
  112. }, 500)
  113. }
  114. }
  115. }
  116. </script>
  117. <style>
  118. /* #ifndef H5 */
  119. /* page {
  120. padding-top: 85px;
  121. } */
  122. /* #endif */
  123. .uni-group {
  124. display: flex;
  125. align-items: center;
  126. }
  127. </style>