添加选项时添加 parent 上下文

This commit is contained in:
liaoxuezhi 2019-11-10 17:22:53 +08:00
parent 7ab84b60e6
commit 2499c6b090
2 changed files with 34 additions and 10 deletions

View File

@ -10,7 +10,8 @@ import {
createObject,
setVariable,
spliceTree,
findTreeIndex
findTreeIndex,
getTree
} from '../../utils/helper';
import {reaction} from 'mobx';
import {FormControlProps, registerFormItem, FormItemBasicConfig} from './Item';
@ -505,9 +506,18 @@ export function registerOptionsControl(config: OptionsConfig) {
}
];
}
const ctx = createObject(
data,
Array.isArray(idx)
? {
parent: getTree(model.options, idx.slice(0, idx.length - 1)),
...value
}
: value
);
let result: any = skipForm
? value
? ctx
: await onOpenDialog(
{
type: 'dialog',
@ -518,19 +528,15 @@ export function registerOptionsControl(config: OptionsConfig) {
controls: addControls
}
},
data
ctx
);
// 单独发请求
if (skipForm && addApi) {
try {
const payload = await env.fetcher(
addApi!,
createObject(data, result),
{
const payload = await env.fetcher(addApi!, result, {
method: 'post'
}
);
});
if (!payload.ok) {
env.notify('error', payload.msg || '新增失败,请仔细检查');

View File

@ -779,6 +779,24 @@ export function findTreeIndex<T extends TreeItem>(
return idx.length ? idx : undefined;
}
export function getTree<T extends TreeItem>(
tree: Array<T>,
idx: Array<number> | number
): T | undefined | null {
const indexes = Array.isArray(idx) ? idx : [idx];
const lastIndex = indexes.pop()!;
let list: Array<T> | null = tree;
for (let i = 0, len = indexes.length; i < len; i++) {
const index = indexes[i];
if (!list![index]) {
list = null;
break;
}
list = list![index].children as any;
}
return list ? list[lastIndex] : undefined;
}
/**
*
*