extends.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import type { Recordable } from '@vben/types';
  2. import type { VxeGridProps, VxeUIExport } from 'vxe-table';
  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. ...customValues,
  42. ...formValues,
  43. },
  44. ...args,
  45. );
  46. return data;
  47. };
  48. api.setState({
  49. gridOptions: {
  50. proxyConfig: {
  51. ajax: {
  52. [key]: wrapperFn,
  53. },
  54. },
  55. },
  56. });
  57. }
  58. export function extendsDefaultFormatter(vxeUI: VxeUIExport) {
  59. vxeUI.formats.add('formatDate', {
  60. tableCellFormatMethod({ cellValue }) {
  61. return formatDate(cellValue);
  62. },
  63. });
  64. vxeUI.formats.add('formatDateTime', {
  65. tableCellFormatMethod({ cellValue }) {
  66. return formatDateTime(cellValue);
  67. },
  68. });
  69. }