| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import type { Recordable } from '@vben/types';
- import type { VxeGridProps, VxeUIExport } from 'vxe-table';
- import type { VxeGridApi } from './api';
- import { formatDate, formatDateTime, isFunction } from '@vben/utils';
- export function extendProxyOptions(
- api: VxeGridApi,
- options: VxeGridProps,
- getFormValues: () => Recordable<any>,
- ) {
- [
- 'query',
- 'querySuccess',
- 'queryError',
- 'queryAll',
- 'queryAllSuccess',
- 'queryAllError',
- ].forEach((key) => {
- extendProxyOption(key, api, options, getFormValues);
- });
- }
- function extendProxyOption(
- key: string,
- api: VxeGridApi,
- options: VxeGridProps,
- getFormValues: () => Recordable<any>,
- ) {
- const { proxyConfig } = options;
- const configFn = (proxyConfig?.ajax as Recordable<any>)?.[key];
- if (!isFunction(configFn)) {
- return options;
- }
- const wrapperFn = async (
- params: Recordable<any>,
- customValues: Recordable<any>,
- ...args: Recordable<any>[]
- ) => {
- const formValues = getFormValues();
- const data = await configFn(
- params,
- {
- ...customValues,
- ...formValues,
- },
- ...args,
- );
- return data;
- };
- api.setState({
- gridOptions: {
- proxyConfig: {
- ajax: {
- [key]: wrapperFn,
- },
- },
- },
- });
- }
- export function extendsDefaultFormatter(vxeUI: VxeUIExport) {
- vxeUI.formats.add('formatDate', {
- tableCellFormatMethod({ cellValue }) {
- return formatDate(cellValue);
- },
- });
- vxeUI.formats.add('formatDateTime', {
- tableCellFormatMethod({ cellValue }) {
- return formatDateTime(cellValue);
- },
- });
- }
|