2024-11-13 22:06:04 +08:00
|
|
|
import type { Recordable } from '@vben/types';
|
2024-10-13 23:44:45 +08:00
|
|
|
import type { VxeGridProps, VxeUIExport } from 'vxe-table';
|
2024-10-10 22:48:25 +08:00
|
|
|
|
|
|
|
import type { VxeGridApi } from './api';
|
|
|
|
|
2024-11-09 15:00:59 +08:00
|
|
|
import { formatDate, formatDateTime, isFunction } from '@vben/utils';
|
2024-10-13 23:44:45 +08:00
|
|
|
|
2024-10-10 22:48:25 +08:00
|
|
|
export function extendProxyOptions(
|
|
|
|
api: VxeGridApi,
|
|
|
|
options: VxeGridProps,
|
2024-11-13 22:06:04 +08:00
|
|
|
getFormValues: () => Recordable<any>,
|
2024-10-10 22:48:25 +08:00
|
|
|
) {
|
|
|
|
[
|
|
|
|
'query',
|
|
|
|
'querySuccess',
|
|
|
|
'queryError',
|
|
|
|
'queryAll',
|
|
|
|
'queryAllSuccess',
|
|
|
|
'queryAllError',
|
|
|
|
].forEach((key) => {
|
|
|
|
extendProxyOption(key, api, options, getFormValues);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function extendProxyOption(
|
|
|
|
key: string,
|
|
|
|
api: VxeGridApi,
|
|
|
|
options: VxeGridProps,
|
2024-11-13 22:06:04 +08:00
|
|
|
getFormValues: () => Recordable<any>,
|
2024-10-10 22:48:25 +08:00
|
|
|
) {
|
|
|
|
const { proxyConfig } = options;
|
2024-11-13 22:06:04 +08:00
|
|
|
const configFn = (proxyConfig?.ajax as Recordable<any>)?.[key];
|
2024-10-10 22:48:25 +08:00
|
|
|
if (!isFunction(configFn)) {
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2024-11-13 22:06:04 +08:00
|
|
|
const wrapperFn = async (
|
|
|
|
params: Recordable<any>,
|
|
|
|
customValues: Recordable<any>,
|
|
|
|
...args: Recordable<any>[]
|
|
|
|
) => {
|
2024-10-10 22:48:25 +08:00
|
|
|
const formValues = getFormValues();
|
2024-11-13 22:06:04 +08:00
|
|
|
const data = await configFn(
|
|
|
|
params,
|
|
|
|
{
|
|
|
|
...customValues,
|
|
|
|
...formValues,
|
|
|
|
},
|
|
|
|
...args,
|
|
|
|
);
|
2024-10-10 22:48:25 +08:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
api.setState({
|
|
|
|
gridOptions: {
|
|
|
|
proxyConfig: {
|
|
|
|
ajax: {
|
|
|
|
[key]: wrapperFn,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2024-10-13 23:44:45 +08:00
|
|
|
|
|
|
|
export function extendsDefaultFormatter(vxeUI: VxeUIExport) {
|
|
|
|
vxeUI.formats.add('formatDate', {
|
|
|
|
tableCellFormatMethod({ cellValue }) {
|
2024-11-09 15:00:59 +08:00
|
|
|
return formatDate(cellValue);
|
2024-10-13 23:44:45 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
vxeUI.formats.add('formatDateTime', {
|
|
|
|
tableCellFormatMethod({ cellValue }) {
|
2024-11-09 15:00:59 +08:00
|
|
|
return formatDateTime(cellValue);
|
2024-10-13 23:44:45 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|