useVxeGrid.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { computed, 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. const { t } = useI18n()
  8. const message = useMessage() // 消息弹窗
  9. interface UseVxeGridConfig<T = any> {
  10. allSchemas: VxeAllSchemas
  11. getListApi: (option: any) => Promise<T>
  12. delListApi?: (option: any) => Promise<T>
  13. exportListApi?: (option: any) => Promise<T>
  14. exportName?: string
  15. }
  16. const appStore = useAppStore()
  17. const currentSize = computed(() => {
  18. let resSize: SizeType = 'small'
  19. const appsize = appStore.getCurrentSize
  20. switch (appsize) {
  21. case 'large':
  22. resSize = 'medium'
  23. break
  24. case 'default':
  25. resSize = 'small'
  26. break
  27. case 'small':
  28. resSize = 'mini'
  29. break
  30. }
  31. return resSize
  32. })
  33. export const useVxeGrid = <T = any>(config?: UseVxeGridConfig<T>) => {
  34. const gridOptions = reactive<VxeGridProps>({
  35. loading: true,
  36. size: currentSize as any,
  37. height: 700,
  38. rowConfig: {
  39. isCurrent: true, // 当鼠标点击行时,是否要高亮当前行
  40. isHover: true // 当鼠标移到行时,是否要高亮当前行
  41. },
  42. toolbarConfig: {
  43. slots: { buttons: 'toolbar_buttons' }
  44. },
  45. printConfig: {
  46. columns: config?.allSchemas.printSchema
  47. },
  48. formConfig: {
  49. enabled: true,
  50. titleWidth: 100,
  51. titleAlign: 'right',
  52. items: config?.allSchemas.searchSchema
  53. },
  54. columns: config?.allSchemas.tableSchema,
  55. pagerConfig: {
  56. border: false, // 带边框
  57. background: true, // 带背景颜色
  58. perfect: true, // 配套的样式
  59. pageSize: 10, // 每页大小
  60. pagerCount: 7, // 显示页码按钮的数量
  61. autoHidden: true, // 当只有一页时自动隐藏
  62. pageSizes: [5, 10, 15, 20, 50, 100, 200, 500], // 每页大小选项列表
  63. layouts: [
  64. 'PrevJump',
  65. 'PrevPage',
  66. 'Jump',
  67. 'PageCount',
  68. 'NextPage',
  69. 'NextJump',
  70. 'Sizes',
  71. 'Total'
  72. ]
  73. },
  74. proxyConfig: {
  75. seq: true, // 启用动态序号代理(分页之后索引自动计算为当前页的起始序号)
  76. form: true, // 启用表单代理,当点击表单提交按钮时会自动触发 reload 行为
  77. props: { result: 'list', total: 'total' },
  78. ajax: {
  79. query: ({ page, form }) => {
  80. const queryParams = Object.assign({}, JSON.parse(JSON.stringify(form)))
  81. queryParams.pageSize = page.pageSize
  82. queryParams.pageNo = page.currentPage
  83. gridOptions.loading = false
  84. return new Promise(async (resolve) => {
  85. resolve(await config?.getListApi(queryParams))
  86. })
  87. },
  88. queryAll: ({ form }) => {
  89. const queryParams = Object.assign({}, JSON.parse(JSON.stringify(form)))
  90. return new Promise(async (resolve) => {
  91. if (config?.exportListApi) {
  92. resolve(await config?.exportListApi(queryParams))
  93. } else {
  94. resolve(await config?.getListApi(queryParams))
  95. }
  96. })
  97. }
  98. }
  99. },
  100. exportConfig: {
  101. filename: config?.exportName,
  102. // 默认选中类型
  103. type: 'csv',
  104. // 自定义数据量列表
  105. modes: ['current', 'all'],
  106. columns: config?.allSchemas.printSchema
  107. }
  108. })
  109. const delList = (ids: string | number | string[] | number[]) => {
  110. return new Promise(async () => {
  111. message.delConfirm().then(() => {
  112. config?.delListApi && config?.delListApi(ids)
  113. message.success(t('common.delSuccess'))
  114. })
  115. })
  116. }
  117. return {
  118. gridOptions,
  119. delList
  120. }
  121. }