useVxeGrid.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. showOverflow: 'tooltip', // 当内容溢出时显示为省略号
  42. tooltipConfig: {
  43. showAll: true // 开启全表工具提示
  44. },
  45. toolbarConfig: {
  46. custom: true,
  47. slots: { buttons: 'toolbar_buttons' }
  48. },
  49. printConfig: {
  50. columns: config?.allSchemas.printSchema
  51. },
  52. formConfig: {
  53. titleWidth: 100,
  54. titleAlign: 'right',
  55. items: config?.allSchemas.searchSchema
  56. },
  57. columns: config?.allSchemas.tableSchema,
  58. pagerConfig: {
  59. border: false, // 带边框
  60. background: true, // 带背景颜色
  61. perfect: true, // 配套的样式
  62. pageSize: 10, // 每页大小
  63. pagerCount: 7, // 显示页码按钮的数量
  64. autoHidden: true, // 当只有一页时自动隐藏
  65. pageSizes: [5, 10, 15, 20, 50, 100, 200, 500], // 每页大小选项列表
  66. layouts: [
  67. 'PrevJump',
  68. 'PrevPage',
  69. 'Jump',
  70. 'PageCount',
  71. 'NextPage',
  72. 'NextJump',
  73. 'Sizes',
  74. 'Total'
  75. ]
  76. },
  77. proxyConfig: {
  78. seq: true, // 启用动态序号代理(分页之后索引自动计算为当前页的起始序号)
  79. form: true, // 启用表单代理,当点击表单提交按钮时会自动触发 reload 行为
  80. props: { result: 'list', total: 'total' },
  81. ajax: {
  82. query: ({ page, form }) => {
  83. const queryParams = Object.assign({}, form)
  84. queryParams.pageSize = page.pageSize
  85. queryParams.pageNo = page.currentPage
  86. gridOptions.loading = false
  87. return new Promise(async (resolve) => {
  88. resolve(await config?.getListApi(queryParams))
  89. })
  90. }
  91. }
  92. }
  93. })
  94. const delList = (ids: string | number | string[] | number[]) => {
  95. return new Promise(async () => {
  96. message.delConfirm().then(() => {
  97. config?.delListApi && config?.delListApi(ids)
  98. message.success(t('common.delSuccess'))
  99. })
  100. })
  101. }
  102. return {
  103. gridOptions,
  104. delList
  105. }
  106. }