list.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view class="fix-top-window">
  3. <view class="uni-header">
  4. <uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
  5. <view class="uni-group">
  6. <input class="uni-search" type="text" v-model="query" @confirm="search"
  7. :placeholder="$t('common.placeholder.query')" />
  8. <button class="uni-button hide-on-phone" type="default" size="mini"
  9. @click="search">{{$t('common.button.search')}}</button>
  10. <button class="uni-button" type="primary" size="mini"
  11. @click="navigateTo('./add')">{{$t('common.button.add')}}</button>
  12. <button class="uni-button" type="warn" size="mini" :disabled="!selectedIndexs.length"
  13. @click="delTable">{{$t('common.button.batchDelete')}}</button>
  14. <!-- #ifdef H5 -->
  15. <download-excel class="hide-on-phone" :fields="exportExcel.fields" :data="exportExcelData"
  16. :type="exportExcel.type" :name="exportExcel.filename">
  17. <button class="uni-button" type="primary" size="mini">{{$t('common.button.exportExcel')}}</button>
  18. </download-excel>
  19. <!-- #endif -->
  20. </view>
  21. </view>
  22. <view class="uni-container">
  23. <unicloud-db ref="udb" collection="uni-id-permissions"
  24. field="permission_id,permission_name,comment,create_date" :where="where" page-data="replace"
  25. :orderby="orderby" :getcount="true" :page-size="options.pageSize" :page-current="options.pageCurrent"
  26. v-slot:default="{data,pagination,loading,error,options}" :options="options" loadtime="manual"
  27. @load="onqueryload">
  28. <uni-table ref="table" :loading="loading" :emptyText="error.message || $t('common.empty')" border stripe
  29. type="selection" @selection-change="selectionChange">
  30. <uni-tr>
  31. <uni-th align="center" filter-type="search"
  32. @filter-change="filterChange($event, 'permission_id')" sortable
  33. @sort-change="sortChange($event, 'permission_id')">权限标识</uni-th>
  34. <uni-th align="center" filter-type="search"
  35. @filter-change="filterChange($event, 'permission_name')" sortable
  36. @sort-change="sortChange($event, 'permission_name')">权限名称</uni-th>
  37. <uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'comment')"
  38. sortable @sort-change="sortChange($event, 'comment')">备注</uni-th>
  39. <uni-th align="center" filter-type="timestamp"
  40. @filter-change="filterChange($event, 'create_date')" sortable
  41. @sort-change="sortChange($event, 'create_date')">创建时间</uni-th>
  42. <uni-th align="center">操作</uni-th>
  43. </uni-tr>
  44. <uni-tr v-for="(item,index) in data" :key="index">
  45. <uni-td align="center">{{item.permission_id}}</uni-td>
  46. <uni-td align="center">{{item.permission_name}}</uni-td>
  47. <uni-td align="center">{{item.comment}}</uni-td>
  48. <uni-td align="center">
  49. <uni-dateformat :threshold="[0, 0]" :date="item.create_date"></uni-dateformat>
  50. </uni-td>
  51. <uni-td align="center">
  52. <view class="uni-group">
  53. <button @click="navigateTo('./edit?id='+item._id, false)" class="uni-button" size="mini"
  54. type="primary">{{$t('common.button.edit')}}</button>
  55. <button @click="confirmDelete(item._id)" class="uni-button" size="mini"
  56. type="warn">{{$t('common.button.delete')}}</button>
  57. </view>
  58. </uni-td>
  59. </uni-tr>
  60. </uni-table>
  61. <view class="uni-pagination-box">
  62. <uni-pagination show-icon show-page-size :page-size="pagination.size" v-model="pagination.current"
  63. :total="pagination.count" @change="onPageChanged" @pageSizeChange="changeSize" />
  64. </view>
  65. </unicloud-db>
  66. </view>
  67. <!-- #ifndef H5 -->
  68. <fix-window />
  69. <!-- #endif -->
  70. </view>
  71. </template>
  72. <script>
  73. import {
  74. enumConverter,
  75. filterToWhere
  76. } from '@/js_sdk/validator/uni-id-permissions.js';
  77. const db = uniCloud.database()
  78. // 表查询配置
  79. const dbOrderBy = 'create_date desc' // 排序字段
  80. const dbSearchFields = ['permission_id', 'permission_name'] // 模糊搜索字段,支持模糊搜索的字段列表
  81. // 分页配置
  82. const pageSize = 20
  83. const pageCurrent = 1
  84. const orderByMapping = {
  85. "ascending": "asc",
  86. "descending": "desc"
  87. }
  88. export default {
  89. data() {
  90. return {
  91. query: '',
  92. where: '',
  93. orderby: dbOrderBy,
  94. orderByFieldName: "",
  95. selectedIndexs: [],
  96. options: {
  97. pageSize,
  98. pageCurrent,
  99. filterData: {},
  100. ...enumConverter
  101. },
  102. imageStyles: {
  103. width: 64,
  104. height: 64
  105. },
  106. exportExcel: {
  107. "filename": "uni-id-permissions.xls",
  108. "type": "xls",
  109. "fields": {
  110. "权限ID": "permission_id",
  111. "权限名称": "permission_name",
  112. "备注": "comment"
  113. }
  114. },
  115. exportExcelData: []
  116. }
  117. },
  118. onLoad() {
  119. this._filter = {}
  120. },
  121. onReady() {
  122. this.$refs.udb.loadData()
  123. },
  124. methods: {
  125. onqueryload(data) {
  126. this.exportExcelData = data
  127. },
  128. changeSize(pageSize) {
  129. this.options.pageSize = pageSize
  130. this.options.pageCurrent = 1
  131. this.$nextTick(() => {
  132. this.loadData()
  133. })
  134. },
  135. getWhere() {
  136. const query = this.query.trim()
  137. if (!query) {
  138. return ''
  139. }
  140. const queryRe = new RegExp(query, 'i')
  141. return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
  142. },
  143. search() {
  144. const newWhere = this.getWhere()
  145. this.where = newWhere
  146. this.$nextTick(() => {
  147. this.loadData()
  148. })
  149. },
  150. loadData(clear = true) {
  151. this.$refs.udb.loadData({
  152. clear
  153. })
  154. },
  155. onPageChanged(e) {
  156. this.selectedIndexs.length = 0
  157. this.$refs.table.clearSelection()
  158. this.$refs.udb.loadData({
  159. current: e.current
  160. })
  161. },
  162. navigateTo(url, clear) {
  163. // clear 表示刷新列表时是否清除页码,true 表示刷新并回到列表第 1 页,默认为 true
  164. uni.navigateTo({
  165. url,
  166. events: {
  167. refreshData: () => {
  168. this.loadData(clear)
  169. }
  170. }
  171. })
  172. },
  173. // 多选处理
  174. selectedItems() {
  175. var dataList = this.$refs.udb.dataList
  176. return this.selectedIndexs.map(i => dataList[i]._id)
  177. },
  178. // 批量删除
  179. delTable() {
  180. this.$refs.udb.remove(this.selectedItems(), {
  181. success: (res) => {
  182. this.$refs.table.clearSelection()
  183. }
  184. })
  185. },
  186. // 多选
  187. selectionChange(e) {
  188. this.selectedIndexs = e.detail.index
  189. },
  190. confirmDelete(id) {
  191. this.$refs.udb.remove(id, {
  192. success: (res) => {
  193. this.$refs.table.clearSelection()
  194. }
  195. })
  196. },
  197. sortChange(e, name) {
  198. this.orderByFieldName = name;
  199. if (e.order) {
  200. this.orderby = name + ' ' + orderByMapping[e.order]
  201. } else {
  202. this.orderby = ''
  203. }
  204. this.$refs.table.clearSelection()
  205. this.$nextTick(() => {
  206. this.$refs.udb.loadData()
  207. })
  208. },
  209. filterChange(e, name) {
  210. this._filter[name] = {
  211. type: e.filterType,
  212. value: e.filter
  213. }
  214. let newWhere = filterToWhere(this._filter, db.command)
  215. if (Object.keys(newWhere).length) {
  216. this.where = newWhere
  217. } else {
  218. this.where = ''
  219. }
  220. this.$nextTick(() => {
  221. this.$refs.udb.loadData()
  222. })
  223. }
  224. }
  225. }
  226. </script>
  227. <style>
  228. </style>