mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-05-06 18:36:44 +08:00
feat(form): 完善select remote功能
This commit is contained in:
parent
e813129eb8
commit
7445d24531
@ -99,11 +99,16 @@ const getOptions = async () => {
|
|||||||
|
|
||||||
const { config } = props;
|
const { config } = props;
|
||||||
const { option } = config;
|
const { option } = config;
|
||||||
let { body } = option;
|
const { root = '', totalKey = 'total' } = option;
|
||||||
|
let { body = {}, url } = option;
|
||||||
|
|
||||||
|
if (typeof url === 'function') {
|
||||||
|
url = await url(mForm, { model: props.model, formValue: mForm?.values });
|
||||||
|
}
|
||||||
|
|
||||||
let postOptions: Record<string, any> = {
|
let postOptions: Record<string, any> = {
|
||||||
method: option.method || 'POST',
|
method: option.method || 'POST',
|
||||||
url: option.url,
|
url,
|
||||||
cache: option.cache,
|
cache: option.cache,
|
||||||
timeout: option.timeout,
|
timeout: option.timeout,
|
||||||
mode: option.mode,
|
mode: option.mode,
|
||||||
@ -111,7 +116,6 @@ const getOptions = async () => {
|
|||||||
json: option.json || false,
|
json: option.json || false,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (body) {
|
|
||||||
if (typeof body === 'function') {
|
if (typeof body === 'function') {
|
||||||
body = body(mForm, {
|
body = body(mForm, {
|
||||||
model: props.model,
|
model: props.model,
|
||||||
@ -124,8 +128,8 @@ const getOptions = async () => {
|
|||||||
body.query = query.value;
|
body.query = query.value;
|
||||||
body.pgSize = pgSize.value;
|
body.pgSize = pgSize.value;
|
||||||
body.pgIndex = pgIndex.value;
|
body.pgIndex = pgIndex.value;
|
||||||
|
|
||||||
postOptions.data = body;
|
postOptions.data = body;
|
||||||
}
|
|
||||||
|
|
||||||
const requestFuc = getConfig('request') as Function;
|
const requestFuc = getConfig('request') as Function;
|
||||||
|
|
||||||
@ -151,10 +155,14 @@ const getOptions = async () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const optionsData = res[option.root];
|
const optionsData = root.split('.').reduce((accumulator, currentValue: any) => accumulator[currentValue], res);
|
||||||
|
|
||||||
if (res.total > 0) {
|
const resTotal = globalThis.parseInt(
|
||||||
total.value = res.total;
|
totalKey.split('.').reduce((accumulator, currentValue: any) => accumulator[currentValue], res),
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
if (resTotal > 0) {
|
||||||
|
total.value = resTotal;
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteData.value = remoteData.value.concat(optionsData);
|
remoteData.value = remoteData.value.concat(optionsData);
|
||||||
@ -214,6 +222,8 @@ const getInitOption = async () => {
|
|||||||
|
|
||||||
const { config } = props;
|
const { config } = props;
|
||||||
const { option } = config;
|
const { option } = config;
|
||||||
|
const { root = '', initRoot = '' } = option;
|
||||||
|
let { initBody = {} } = option;
|
||||||
|
|
||||||
let options: SelectOption[] | SelectGroupOption[] = [];
|
let options: SelectOption[] | SelectGroupOption[] = [];
|
||||||
|
|
||||||
@ -226,24 +236,53 @@ const getInitOption = async () => {
|
|||||||
url = await url(mForm, { model: props.model, formValue: mForm?.values });
|
url = await url(mForm, { model: props.model, formValue: mForm?.values });
|
||||||
}
|
}
|
||||||
|
|
||||||
const postOptions: Record<string, any> = {
|
if (typeof initBody === 'function') {
|
||||||
|
initBody = initBody(mForm, {
|
||||||
|
model: props.model,
|
||||||
|
formValue: mForm?.values,
|
||||||
|
formValues: mForm?.values,
|
||||||
|
config: props.config,
|
||||||
|
}) as Record<string, any>;
|
||||||
|
}
|
||||||
|
|
||||||
|
let postOptions: Record<string, any> = {
|
||||||
method: option.method || 'POST',
|
method: option.method || 'POST',
|
||||||
url,
|
url,
|
||||||
data: {
|
data: {
|
||||||
id: props.model[props.name],
|
id: props.model[props.name],
|
||||||
|
...initBody,
|
||||||
},
|
},
|
||||||
mode: option.mode,
|
mode: option.mode,
|
||||||
headers: option.headers || {},
|
headers: option.headers || {},
|
||||||
json: option.json || false,
|
json: option.json || false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (typeof option.beforeInitRequest === 'function') {
|
||||||
|
postOptions = option.beforeInitRequest(mForm, postOptions, {
|
||||||
|
model: props.model,
|
||||||
|
formValue: mForm?.values,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (option.method?.toLocaleLowerCase() === 'jsonp') {
|
if (option.method?.toLocaleLowerCase() === 'jsonp') {
|
||||||
postOptions.jsonpCallback = option.jsonpCallback || 'callback';
|
postOptions.jsonpCallback = option.jsonpCallback || 'callback';
|
||||||
}
|
}
|
||||||
|
|
||||||
const requestFuc = getConfig('request') as Function;
|
const requestFuc = getConfig('request') as Function;
|
||||||
const res = await requestFuc(postOptions);
|
let res = await requestFuc(postOptions);
|
||||||
|
|
||||||
let initData = res[option.root];
|
if (typeof option.afterRequest === 'function') {
|
||||||
|
res = option.afterRequest(mForm, res, {
|
||||||
|
model: props.model,
|
||||||
|
formValue: mForm?.values,
|
||||||
|
formValues: mForm?.values,
|
||||||
|
config: props.config,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let initData = (initRoot || root)
|
||||||
|
.split('.')
|
||||||
|
.reduce((accumulator, currentValue: any) => accumulator[currentValue], res);
|
||||||
if (initData) {
|
if (initData) {
|
||||||
if (!Array.isArray(initData)) {
|
if (!Array.isArray(initData)) {
|
||||||
initData = [initData];
|
initData = [initData];
|
||||||
|
@ -433,7 +433,7 @@ export interface SelectConfig extends FormItem, Input {
|
|||||||
options: SelectConfigOption[] | SelectConfigGroupOption[] | SelectOptionFunction;
|
options: SelectConfigOption[] | SelectConfigGroupOption[] | SelectOptionFunction;
|
||||||
remote: true;
|
remote: true;
|
||||||
option: {
|
option: {
|
||||||
url: string;
|
url: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string);
|
||||||
initUrl?: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string);
|
initUrl?: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string);
|
||||||
method?: 'jsonp' | string;
|
method?: 'jsonp' | string;
|
||||||
cache?: boolean;
|
cache?: boolean;
|
||||||
@ -444,13 +444,22 @@ export interface SelectConfig extends FormItem, Input {
|
|||||||
};
|
};
|
||||||
json?: false | boolean;
|
json?: false | boolean;
|
||||||
body?: Record<string, any> | RemoteSelectOptionBodyFunction;
|
body?: Record<string, any> | RemoteSelectOptionBodyFunction;
|
||||||
|
initBody?: Record<string, any> | RemoteSelectOptionBodyFunction;
|
||||||
jsonpCallback?: 'callback' | string;
|
jsonpCallback?: 'callback' | string;
|
||||||
afterRequest?: RemoteSelectOptionRequestFunction;
|
afterRequest?: RemoteSelectOptionRequestFunction;
|
||||||
|
afterInitRequest?: RemoteSelectOptionRequestFunction;
|
||||||
beforeRequest?: (mForm: FormState | undefined, postOptions: Record<string, any>, data: any) => Record<string, any>;
|
beforeRequest?: (mForm: FormState | undefined, postOptions: Record<string, any>, data: any) => Record<string, any>;
|
||||||
root: string;
|
beforeInitRequest?: (
|
||||||
|
mForm: FormState | undefined,
|
||||||
|
postOptions: Record<string, any>,
|
||||||
|
data: any,
|
||||||
|
) => Record<string, any>;
|
||||||
|
root?: string;
|
||||||
|
totalKey?: string;
|
||||||
|
initRoot?: string;
|
||||||
item?: RemoteSelectOptionItemFunction;
|
item?: RemoteSelectOptionItemFunction;
|
||||||
value: string | SelectOptionValueFunction;
|
value?: string | SelectOptionValueFunction;
|
||||||
text: string | SelectOptionTextFunction;
|
text?: string | SelectOptionTextFunction;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user