Skip to main content

给字段添加过滤条件

在我们的产品中,表单中参照引用字段的条件是我们系统开发预置好的。用户无法添加条件来满足更精细的需求。那么,我们可以通过 Client API 提供的能力来满足这个需求。

给表头字段添加过滤条件

下面的脚本演示了给报销单的表头字段 申请部门 添加一个过滤条件。

thisApp.formOnLoad = async (ctx) => {
const form = ctx.getFormContext().form;

form.getField('applyDepartment').addFilter({
name: {
like: '研发部'
}
})
};

给子表中的字段添加过滤条件

给子表中的字段添加过滤条件和给表头的字段添加过滤条件有点不同。需要用到 subGridRowOnInit这个入口。

下面的脚本演示了我们给报销单的长途子表中的 费用 添加一个条件。

thisApp.subGridRowOnInit = (ctx) => {
const form = ctx.getFormContext().form;
const currentCtx = ctx.getCurrentContext();
const { subGridName, currentField } = ctx.getCurrentContext();

if (subGridName === 'reimburseTransports') {
currentField.getField('cost').addFilter({
code: {
startsWith: '4001', // 编码以 "4001" 开头的
}
})
}
};