list.vue 7.2 KB

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