Skip to main content

Fill in the subform with multiple select subforms associated with the form

1. Usage scenarios​

This example describes how to fill the subform data of other pages into the subform of the current page on the YIDA form page.

2. Implement functions​

2.1. Create form page​

2.2. Configure a data source​

Reference documents:Query Form instance details based on form instance ID

The interface configuration is as follows:

2.3. Copy the following code to Page JS​

Note that you modify the values of the following option parameters.

export async function onChange({ value }) {
const option = {
target: 'tableField_lbafn35n', // 目标子表单组件唯一标识
current: 'tableField_lbaft5oa', // 当前子表单组件唯一标识
relation: [
// 字段映射关系 targetField:目标子表单字段唯一标识, currentField:当前子表单字段唯一标识
{
targetField: 'textField_lbafn35q',
currentField: 'textField_lbaft5ob' // 菜品名
}, {
targetField: 'numberField_lbafn35r',
currentField: 'numberField_lbaft5oc' // 价格
}
]
};
const { target, current, relation } = option;
if (value.length) {
const closeLoading = this.utils.toast({
title: '数据获取中...',
type: 'loading'
});
const tableData = [];
const failedInstId = [];
for (let i = 0; i < value.length; i++) {
const delay = i === 0 ? 0 : 200;
await new Promise((resolve => {
// 注意节流,每次请求间隔 200 ms
setTimeout(async () => {
try {
await this.dataSourceMap.getDataById.load({
formInstId: value[i].instanceId
}).then(res => {
tableData.push(...res.formData[target]);
}).catch(error => {
closeLoading();
this.utils.toast({
title: error.message,
type: 'error'
});
});
} catch (e) {
// 把失败的实例 id 收集起来
failedInstId.push(value[i].instanceId);
}
resolve();
}, delay);
}));
};
const result = [];
for (let j = 0; j < tableData.length; j++) {
let itemObj = {}
for (let k = 0; k < relation.length; k++) {
itemObj[relation[k].currentField] = tableData[j][relation[k].targetField]
};
result.push(itemObj);
};
closeLoading();
this.$(current).setValue(result);
} else {
this.$(current).reset();
};
}

2.4. Component event binding​

3. Effect​

4. Try it online​

5. Upgrade the associated form function​

This doc is generated using machine translation. Any discrepancies or differences created in the translation are not binding and have no legal effect for compliance or enforcement purposes.
© 2014–2025 DingTalk Technology Co., Ltd