useVxeGrid.ts 3.0 KB

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