table.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. <!-- 输入框 -->
  9. <input class="uni-search" type="text" v-model="searchVal" @confirm="search" :placeholder="$t('common.placeholder.query')" />
  10. <!-- 搜索按钮 -->
  11. <button class="uni-button" type="default" size="mini" @click="search">{{$t('common.button.search')}}</button>
  12. <!-- 添加按钮 -->
  13. <button class="uni-button" type="primary" size="mini">{{$t('common.button.add')}}</button>
  14. <!-- 批量删除按钮 -->
  15. <button class="uni-button" type="warn" size="mini" @click="delTable">{{$t('common.button.batchDelete')}}</button>
  16. </view>
  17. </view>
  18. <view class="uni-container">
  19. <!-- 表格组件 -->
  20. <uni-table :loading="loading" border stripe type="selection" :emptyText="$t('common.empty')" @selection-change="selectionChange">
  21. <uni-tr>
  22. <!-- 表头列 -->
  23. <uni-th width="150" align="center">日期</uni-th>
  24. <uni-th width="150" align="center">姓名</uni-th>
  25. <uni-th align="center">地址</uni-th>
  26. <uni-th width="204" align="center">设置</uni-th>
  27. </uni-tr>
  28. <uni-tr v-for="(item ,index) in tableData" :key="index">
  29. <!-- 表格数据列 -->
  30. <uni-td>{{item.date}}</uni-td>
  31. <uni-td>
  32. <view class="name">{{item.name}}</view>
  33. </uni-td>
  34. <uni-td>{{item.address}}</uni-td>
  35. <uni-td>
  36. <view class="uni-group">
  37. <!-- 编辑按钮 -->
  38. <button class="uni-button" size="mini" type="primary">{{$t('common.button.edit')}}</button>
  39. <!-- 删除按钮 -->
  40. <button class="uni-button" size="mini" type="warn">{{$t('common.button.delete')}}</button>
  41. </view>
  42. </uni-td>
  43. </uni-tr>
  44. </uni-table>
  45. <view class="uni-pagination-box">
  46. <!-- 分页组件 -->
  47. <uni-pagination show-icon :page-size="pageSize" :current="pageCurrent" :total="total" @change="change" />
  48. </view>
  49. </view>
  50. <!-- #ifndef H5 -->
  51. <fix-window />
  52. <!-- #endif -->
  53. </view>
  54. </template>
  55. <script>
  56. // 导入名为 "tableData" 的模块,路径为 './tableData.js'
  57. import tableData from './tableData.js'
  58. // 导出默认模块
  59. export default {
  60. // 数据属性
  61. data() {
  62. return {
  63. // 搜索值
  64. searchVal: '',
  65. // 表格数据
  66. tableData: [],
  67. // 每页数据量
  68. pageSize: 10,
  69. // 当前页
  70. pageCurrent: 1,
  71. // 数据总量
  72. total: 0,
  73. // 加载状态
  74. loading: false
  75. }
  76. },
  77. // 页面加载时的处理函数
  78. onLoad() {
  79. // 重置选中项数组
  80. this.selectedIndexs = []
  81. // 获取第一页数据
  82. this.getData(1)
  83. },
  84. // 方法
  85. methods: {
  86. // 多选处理
  87. selectedItems() {
  88. return this.selectedIndexs.map(i => this.tableData[i])
  89. },
  90. // 多选事件处理函数
  91. selectionChange(e) {
  92. this.selectedIndexs = e.detail.index
  93. },
  94. // 批量删除函数
  95. delTable() {
  96. this.selectedItems();
  97. },
  98. // 分页触发事件处理函数
  99. change(e) {
  100. this.getData(e.current)
  101. },
  102. // 搜索函数
  103. search() {
  104. this.getData(1, this.searchVal)
  105. },
  106. // 获取数据函数
  107. getData(pageCurrent, value = "") {
  108. this.loading = true
  109. this.pageCurrent = pageCurrent
  110. this.request({
  111. pageSize: this.pageSize,
  112. pageCurrent: pageCurrent,
  113. value: value,
  114. success: (res) => {
  115. this.tableData = res.data
  116. this.total = res.total
  117. this.loading = false
  118. }
  119. })
  120. },
  121. // 伪request请求函数
  122. request(options) {
  123. const {
  124. pageSize,
  125. pageCurrent,
  126. success,
  127. value
  128. } = options
  129. let total = tableData.length
  130. let data = tableData.filter((item, index) => {
  131. const idx = index - (pageCurrent - 1) * pageSize
  132. return idx < pageSize && idx >= 0
  133. })
  134. if (value) {
  135. data = []
  136. tableData.forEach(item => {
  137. if (item.name.indexOf(value) !== -1) {
  138. data.push(item)
  139. }
  140. })
  141. total = data.length
  142. }
  143. setTimeout(() => {
  144. typeof success === 'function' && success({
  145. data: data,
  146. total: total
  147. })
  148. }, 500)
  149. }
  150. }
  151. }
  152. </script>
  153. <style>
  154. /* #ifndef H5 */
  155. page {
  156. padding-top: 85px;
  157. }
  158. /* #endif */
  159. </style>