extends.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type { VxeGridProps, VxeUIExport } from 'vxe-table';
  2. import type { Recordable } from '@vben/types';
  3. import type { VxeGridApi } from './api';
  4. import { formatDate, formatDateTime, isFunction } from '@vben/utils';
  5. export function extendProxyOptions(
  6. api: VxeGridApi,
  7. options: VxeGridProps,
  8. getFormValues: () => Recordable<any>,
  9. ) {
  10. [
  11. 'query',
  12. 'querySuccess',
  13. 'queryError',
  14. 'queryAll',
  15. 'queryAllSuccess',
  16. 'queryAllError',
  17. ].forEach((key) => {
  18. extendProxyOption(key, api, options, getFormValues);
  19. });
  20. }
  21. function extendProxyOption(
  22. key: string,
  23. api: VxeGridApi,
  24. options: VxeGridProps,
  25. getFormValues: () => Recordable<any>,
  26. ) {
  27. const { proxyConfig } = options;
  28. const configFn = (proxyConfig?.ajax as Recordable<any>)?.[key];
  29. if (!isFunction(configFn)) {
  30. return options;
  31. }
  32. const wrapperFn = async (
  33. params: Recordable<any>,
  34. customValues: Recordable<any>,
  35. ...args: Recordable<any>[]
  36. ) => {
  37. const formValues = getFormValues();
  38. const data = await configFn(
  39. params,
  40. {
  41. /**
  42. * 开启toolbarConfig.refresh功能
  43. * 点击刷新按钮 这里的值为PointerEvent 会携带错误参数
  44. */
  45. ...(customValues instanceof PointerEvent ? {} : customValues),
  46. ...formValues,
  47. },
  48. ...args,
  49. );
  50. return data;
  51. };
  52. api.setState({
  53. gridOptions: {
  54. proxyConfig: {
  55. ajax: {
  56. [key]: wrapperFn,
  57. },
  58. },
  59. },
  60. });
  61. }
  62. export function extendsDefaultFormatter(vxeUI: VxeUIExport) {
  63. vxeUI.formats.add('formatDate', {
  64. tableCellFormatMethod({ cellValue }) {
  65. return formatDate(cellValue);
  66. },
  67. });
  68. vxeUI.formats.add('formatDateTime', {
  69. tableCellFormatMethod({ cellValue }) {
  70. return formatDateTime(cellValue);
  71. },
  72. });
  73. }