useVxeGrid.ts 5.7 KB

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