list.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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="collectionList"
  21. :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. collectionList: [ db.collection('uni-id-roles').field('comment,permission,role_id,role_name,create_date').getTemp(),db.collection('uni-id-permissions').field('permission_name, permission_id').getTemp() ],
  88. query: '',
  89. where: '',
  90. orderby: dbOrderBy,
  91. orderByFieldName: "",
  92. selectedIndexs: [],
  93. options: {
  94. pageSize,
  95. pageCurrent,
  96. filterData: {},
  97. ...enumConverter
  98. },
  99. imageStyles: {
  100. width: 64,
  101. height: 64
  102. },
  103. exportExcel: {
  104. "filename": "uni-id-roles.xls",
  105. "type": "xls",
  106. "fields": {
  107. "唯一ID": "role_id",
  108. "名称": "role_name",
  109. "权限": "permission",
  110. "备注": "comment",
  111. "create_date": "create_date"
  112. }
  113. },
  114. exportExcelData: []
  115. }
  116. },
  117. onLoad() {
  118. this._filter = {}
  119. },
  120. onReady() {
  121. this.$refs.udb.loadData()
  122. },
  123. methods: {
  124. onqueryload(data) {
  125. for (let i = 0; i < data.length; i++) {
  126. let item = data[i]
  127. item.permission = item.permission.map(pItem => pItem.permission_name).join('、')
  128. item.create_date = this.$formatDate(item.create_date)
  129. }
  130. this.exportExcelData = data
  131. },
  132. changeSize(pageSize) {
  133. this.options.pageSize = pageSize
  134. this.options.pageCurrent = 1
  135. this.$nextTick(() => {
  136. this.loadData()
  137. })
  138. },
  139. getWhere() {
  140. const query = this.query.trim()
  141. if (!query) {
  142. return ''
  143. }
  144. const queryRe = new RegExp(query, 'i')
  145. return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
  146. },
  147. search() {
  148. const newWhere = this.getWhere()
  149. this.where = newWhere
  150. this.$nextTick(() => {
  151. this.loadData()
  152. })
  153. },
  154. loadData(clear = true) {
  155. this.$refs.udb.loadData({
  156. clear
  157. })
  158. },
  159. onPageChanged(e) {
  160. this.selectedIndexs.length = 0
  161. this.$refs.table.clearSelection()
  162. this.$refs.udb.loadData({
  163. current: e.current
  164. })
  165. },
  166. navigateTo(url, clear) {
  167. // clear 表示刷新列表时是否清除页码,true 表示刷新并回到列表第 1 页,默认为 true
  168. uni.navigateTo({
  169. url,
  170. events: {
  171. refreshData: () => {
  172. this.loadData(clear)
  173. }
  174. }
  175. })
  176. },
  177. // 多选处理
  178. selectedItems() {
  179. let dataList = this.$refs.udb.dataList
  180. return this.selectedIndexs.map(i => dataList[i]._id)
  181. },
  182. // 批量删除
  183. delTable() {
  184. this.$refs.udb.remove(this.selectedItems(), {
  185. success: (res) => {
  186. this.$refs.table.clearSelection()
  187. }
  188. })
  189. },
  190. // 多选
  191. selectionChange(e) {
  192. this.selectedIndexs = e.detail.index
  193. },
  194. confirmDelete(id) {
  195. this.$refs.udb.remove(id, {
  196. success: (res) => {
  197. this.$refs.table.clearSelection()
  198. }
  199. })
  200. },
  201. sortChange(e, name) {
  202. this.orderByFieldName = name;
  203. if (e.order) {
  204. this.orderby = name + ' ' + orderByMapping[e.order]
  205. } else {
  206. this.orderby = ''
  207. }
  208. this.$refs.table.clearSelection()
  209. this.$nextTick(() => {
  210. this.$refs.udb.loadData()
  211. })
  212. },
  213. filterChange(e, name) {
  214. this._filter[name] = {
  215. type: e.filterType,
  216. value: e.filter
  217. }
  218. let newWhere = filterToWhere(this._filter, db.command)
  219. if (Object.keys(newWhere).length) {
  220. this.where = newWhere
  221. } else {
  222. this.where = ''
  223. }
  224. this.$nextTick(() => {
  225. this.$refs.udb.loadData()
  226. })
  227. }
  228. }
  229. }
  230. </script>
  231. <style>
  232. </style>