table.vue 5.1 KB

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