useVxeGrid.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { computed, nextTick, reactive } from 'vue'
  2. import { SizeType, VxeGridProps } from 'vxe-table'
  3. import { useAppStore } from '@/store/modules/app'
  4. import { VxeAllSchemas } from './useVxeCrudSchemas'
  5. import { useI18n } from '@/hooks/web/useI18n'
  6. import { useMessage } from '@/hooks/web/useMessage'
  7. import download from '@/utils/download'
  8. const { t } = useI18n()
  9. const message = useMessage() // 消息弹窗
  10. interface UseVxeGridConfig<T = any> {
  11. allSchemas: VxeAllSchemas
  12. getListApi: (option: any) => Promise<T>
  13. deleteApi?: (option: any) => Promise<T>
  14. exportListApi?: (option: any) => Promise<T>
  15. exportName?: string // 导出文件夹名称
  16. queryParams?: any // 其他查询参数
  17. }
  18. const appStore = useAppStore()
  19. const currentSize = computed(() => {
  20. let resSize: SizeType = 'small'
  21. const appsize = appStore.getCurrentSize
  22. switch (appsize) {
  23. case 'large':
  24. resSize = 'medium'
  25. break
  26. case 'default':
  27. resSize = 'small'
  28. break
  29. case 'small':
  30. resSize = 'mini'
  31. break
  32. }
  33. return resSize
  34. })
  35. export const useVxeGrid = <T = any>(config?: UseVxeGridConfig<T>) => {
  36. /**
  37. * grid options 初始化
  38. */
  39. const gridOptions = reactive<VxeGridProps>({
  40. loading: true,
  41. size: currentSize as any,
  42. height: 800,
  43. rowConfig: {
  44. isCurrent: true, // 当鼠标点击行时,是否要高亮当前行
  45. isHover: true // 当鼠标移到行时,是否要高亮当前行
  46. },
  47. toolbarConfig: {
  48. slots: { buttons: 'toolbar_buttons' }
  49. },
  50. printConfig: {
  51. columns: config?.allSchemas.printSchema
  52. },
  53. formConfig: {
  54. enabled: true,
  55. titleWidth: 100,
  56. titleAlign: 'right',
  57. items: config?.allSchemas.searchSchema
  58. },
  59. columns: config?.allSchemas.tableSchema,
  60. pagerConfig: {
  61. border: false, // 带边框
  62. background: true, // 带背景颜色
  63. perfect: false, // 配套的样式
  64. pageSize: 10, // 每页大小
  65. pagerCount: 7, // 显示页码按钮的数量
  66. autoHidden: true, // 当只有一页时自动隐藏
  67. pageSizes: [5, 10, 20, 30, 50, 100], // 每页大小选项列表
  68. layouts: [
  69. 'PrevJump',
  70. 'PrevPage',
  71. 'JumpNumber',
  72. 'NextPage',
  73. 'NextJump',
  74. 'Sizes',
  75. 'FullJump',
  76. 'Total'
  77. ]
  78. },
  79. proxyConfig: {
  80. seq: true, // 启用动态序号代理(分页之后索引自动计算为当前页的起始序号)
  81. form: true, // 启用表单代理,当点击表单提交按钮时会自动触发 reload 行为
  82. props: { result: 'list', total: 'total' },
  83. ajax: {
  84. query: ({ page, form }) => {
  85. let queryParams: any = Object.assign({}, JSON.parse(JSON.stringify(form)))
  86. if (config?.queryParams) {
  87. queryParams = Object.assign(queryParams, config.queryParams)
  88. }
  89. queryParams.pageSize = page.pageSize
  90. queryParams.pageNo = page.currentPage
  91. gridOptions.loading = false
  92. return new Promise(async (resolve) => {
  93. resolve(await config?.getListApi(queryParams))
  94. })
  95. },
  96. queryAll: ({ form }) => {
  97. const queryParams = Object.assign({}, JSON.parse(JSON.stringify(form)))
  98. return new Promise(async (resolve) => {
  99. if (config?.exportListApi) {
  100. resolve(await config?.exportListApi(queryParams))
  101. } else {
  102. resolve(await config?.getListApi(queryParams))
  103. }
  104. })
  105. }
  106. }
  107. },
  108. exportConfig: {
  109. filename: config?.exportName,
  110. // 默认选中类型
  111. type: 'csv',
  112. // 自定义数据量列表
  113. modes: ['current', 'all'],
  114. columns: config?.allSchemas.printSchema
  115. }
  116. })
  117. /**
  118. * 刷新列表
  119. * @param ref
  120. * @returns
  121. */
  122. const getList = async (ref) => {
  123. if (!ref) {
  124. console.error('未传入gridRef')
  125. return
  126. }
  127. await nextTick()
  128. ref.value.commitProxy('query')
  129. }
  130. // 获取查询参数
  131. const getSearchData = async (ref) => {
  132. if (!ref) {
  133. console.error('未传入gridRef')
  134. return
  135. }
  136. await nextTick()
  137. const queryParams = Object.assign(
  138. {},
  139. JSON.parse(JSON.stringify(ref.value.getProxyInfo()?.form))
  140. )
  141. return queryParams
  142. }
  143. /**
  144. * 删除
  145. * @param ref
  146. * @param ids rowid
  147. * @returns
  148. */
  149. const deleteData = async (ref, ids: string | number) => {
  150. if (!ref) {
  151. console.error('未传入gridRef')
  152. return
  153. }
  154. if (!config?.deleteApi) {
  155. console.error('未传入delListApi')
  156. return
  157. }
  158. await nextTick()
  159. return new Promise(async () => {
  160. message
  161. .delConfirm()
  162. .then(() => {
  163. config?.deleteApi && config?.deleteApi(ids)
  164. message.success(t('common.delSuccess'))
  165. })
  166. .finally(async () => {
  167. // 刷新列表
  168. ref.value.commitProxy('query')
  169. })
  170. })
  171. }
  172. /**
  173. * 导出
  174. * @param ref
  175. * @param fileName 文件名,默认excel.xls
  176. * @returns
  177. */
  178. const exportList = async (ref, fileName?: string) => {
  179. if (!ref) {
  180. console.error('未传入gridRef')
  181. return
  182. }
  183. if (!config?.exportListApi) {
  184. console.error('未传入exportListApi')
  185. return
  186. }
  187. await nextTick()
  188. const queryParams = Object.assign(
  189. {},
  190. JSON.parse(JSON.stringify(ref.value?.getProxyInfo()?.form))
  191. )
  192. message.exportConfirm().then(async () => {
  193. const res = await (config?.exportListApi && config?.exportListApi(queryParams))
  194. download.excel(res as unknown as Blob, fileName ? fileName : 'excel.xls')
  195. })
  196. }
  197. /**
  198. * 表格最大/最小化
  199. * @param ref
  200. * @returns
  201. */
  202. const zoom = async (ref) => {
  203. if (!ref) {
  204. console.error('未传入gridRef')
  205. return
  206. }
  207. await nextTick()
  208. ref.value.zoom(!ref.value.isMaximized())
  209. }
  210. return {
  211. gridOptions,
  212. getList,
  213. getSearchData,
  214. deleteData,
  215. exportList,
  216. zoom
  217. }
  218. }