list.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <view>
  3. <view class="uni-header">
  4. <view class="uni-group">
  5. <view class="uni-title"></view>
  6. <view class="uni-sub-title"></view>
  7. </view>
  8. <view class="uni-group">
  9. <input class="uni-search" type="text" v-model="query" placeholder="用户/内容" />
  10. <button class="uni-button" type="default" size="mini" @click="search">搜索</button>
  11. </view>
  12. </view>
  13. <view class="uni-container">
  14. <unicloud-db ref="udb" :collection="collectionName" :options="options" :where="where" page-data="replace" :orderby="orderby"
  15. :getcount="true" :page-size="options.pageSize" :page-current="options.pageCurrent" v-slot:default="{data,pagination,loading,error}">
  16. <uni-table :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe >
  17. <uni-tr>
  18. <uni-th align="center">序号</uni-th>
  19. <uni-th align="center">用户</uni-th>
  20. <uni-th align="center">内容</uni-th>
  21. <uni-th align="center">IP</uni-th>
  22. <uni-th align="center">时间</uni-th>
  23. </uni-tr>
  24. <uni-tr v-for="(item,index) in data" :key="index">
  25. <uni-td align="center">{{(pagination.current -1)*pagination.size + (index+1)}}</uni-td>
  26. <uni-td align="center">{{item.user_name}}</uni-td>
  27. <uni-td align="center">{{item.content}}</uni-td>
  28. <uni-td align="center">{{item.ip}}</uni-td>
  29. <uni-td align="center">
  30. <uni-dateformat :date="item.create_date" :threshold="[0, 0]" />
  31. </uni-td>
  32. </uni-tr>
  33. </uni-table>
  34. <view class="uni-pagination-box">
  35. <uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current" :total="pagination.count"
  36. @change="onPageChanged" />
  37. </view>
  38. </unicloud-db>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. const db = uniCloud.database()
  44. // 表查询配置
  45. const dbCollectionName = 'opendb-admin-log'
  46. const dbOrderBy = 'create_date' // 排序字段
  47. const dbSearchFields = ["user_name","content"] // 支持模糊搜索的字段列表
  48. // 分页配置
  49. const pageSize = 20
  50. const pageCurrent = 1
  51. export default {
  52. data() {
  53. return {
  54. query: '',
  55. where: '',
  56. orderby: dbOrderBy,
  57. collectionName: dbCollectionName,
  58. options: {
  59. pageSize,
  60. pageCurrent
  61. }
  62. }
  63. },
  64. methods: {
  65. getWhere() {
  66. const query = this.query.trim()
  67. if (!query) {
  68. return ''
  69. }
  70. const queryRe = new RegExp(query, 'i')
  71. return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
  72. },
  73. search() {
  74. const newWhere = this.getWhere()
  75. const isSameWhere = newWhere === this.where
  76. this.where = newWhere
  77. if (isSameWhere) { // 相同条件时,手动强制刷新
  78. this.loadData()
  79. }
  80. },
  81. loadData(clear = true) {
  82. this.$refs.udb.loadData({
  83. clear
  84. })
  85. },
  86. onPageChanged(e) {
  87. this.$refs.udb.loadData({
  88. current: e.current
  89. })
  90. },
  91. navigateTo(url) {
  92. uni.navigateTo({
  93. url,
  94. events: {
  95. refreshData: () => {
  96. this.loadData()
  97. }
  98. }
  99. })
  100. },
  101. // 多选处理
  102. selectedItems() {
  103. var dataList = this.$refs.udb.dataList
  104. return this.selectedIndexs.map(i => dataList[i]._id)
  105. },
  106. //批量删除
  107. delTable() {
  108. this.$refs.udb.remove(this.selectedItems())
  109. },
  110. // 多选
  111. selectionChange(e) {
  112. this.selectedIndexs = e.detail.index
  113. },
  114. confirmDelete(id) {
  115. this.$refs.udb.remove(id)
  116. }
  117. }
  118. }
  119. </script>
  120. <style>
  121. </style>