由于表达式内的函数在前后端都可能执行,所以同一个表达式需要前后端同时定义
后端表达式自定义
package pro.shushi.pamirs.demo.core.fun;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import pro.shushi.pamirs.meta.annotation.Fun;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.common.constants.NamespaceConstants;
import java.util.Date;
import java.util.List;
import static pro.shushi.pamirs.meta.enmu.FunctionCategoryEnum.TEXT;
import static pro.shushi.pamirs.meta.enmu.FunctionCategoryEnum.TIME;
import static pro.shushi.pamirs.meta.enmu.FunctionLanguageEnum.JAVA;
import static pro.shushi.pamirs.meta.enmu.FunctionOpenEnum.LOCAL;
import static pro.shushi.pamirs.meta.enmu.FunctionSceneEnum.EXPRESSION;
/**
* 自定义函数
*/
@Fun(NamespaceConstants.expression)
public class DemoCustomFunctions {
@Function.Advanced(
displayName = "逗号分隔字符串数组", language = JAVA,
builtin = true, category = TEXT
)
@Function.fun("MY_COMMA")
@Function(name = "MY_COMMA", scene = {EXPRESSION}, openLevel = LOCAL,
summary = "函数示例: MY_COMMA(list)\n函数说明: 将字符串数组转为逗号分隔的字符串"
)
public String myComma(List<String> list) {
return StringUtils.join(list, ",");
}
@Function.Advanced(
displayName = "根据出生算年龄", language = JAVA,
builtin = true, category = TIME
)
@Function.fun("CALC_AGE")
@Function(name = "CALC_AGE", scene = {EXPRESSION}, openLevel = LOCAL,
summary = "函数示例: CALC_AGE(birthDate)\n函数说明: 根据出生算年龄"
)
public Integer calcAge(Date birthDate) {
if (birthDate == null) {
return 0;
}
return new DateTime().getYear() - new DateTime(birthDate).getYear();
}
}
前端表达式定义
定义后导入到main.ts里,导入@kunlun/dependencies
的代码之后
import { Expression } from '@kunlun/dependencies';
import dayjs from 'dayjs';
Expression.getInstance().registerFunction('MY_COMMA', ['array'], (list: string[]) => {
return (list || [])
.map((value) => {
return `'${value}'`;
})
.join(',');
});
Expression.getInstance().registerFunction('CALC_AGE', ['string'], (birthDate: string) => {
if (birthDate == null) {
return 0;
}
return Math.ceil(dayjs().year() - dayjs(birthDate, 'YYYY-MM-DD HH:mm:ss').year());
});
界面设计器拖入年龄的非存储字段,属性面板的计算公式
的函数中选择自定义的函数
注意:不能在时间类型字段的计算公式使用该函数,因为计算后的结果为数字类型,与原字段不一致
表格页可以看到根据发布时间计算的年龄,与预期一致
Oinone社区 作者:nation原创文章,如若转载,请注明出处:https://doc.oinone.top/install/frontendinstall/14564.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验