useVxeGrid.ts 3.1 KB

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