Browse Source

自定义函数设置下拉树默认值-https://help.finereport.com/doc-view-3637.html

文档中用到的自定义函数 TREENODEFINDER Java脚本代码
函数用途:通过「自定义函数」实现根据子节点的值找出叶子节点的完整路径,从而实现下拉树控件默认值的简单设置。

注:此方法只能用于构建方式为「自动构建」的下拉树控件。

函数简介:
函数:TREENODEFINDER(param1,param2)
释义 :
1)param1 为叶子节点的值,可以是显示值,也可以是实际值。
传入单个值时,以 value 格式传入。
传入单个值时,以数组格式 ARRAY(value1,value2,.....) 传入。
   value 为字符串时需要加 "" 。
2)param2 为构建下拉树控件的树数据集名称,值格式为“TreeName”。
示例:
单值:TREENODEFINDER("部长","Tree1")
多值:TREENODEFINDER(ARRAY("部长","会计"), "Tree1")
release/10.0
Rosie 4 years ago
parent
commit
a6372ce1d7
  1. 126
      src/main/java/com/fr/function/TREENODEFINDER.java

126
src/main/java/com/fr/function/TREENODEFINDER.java

@ -0,0 +1,126 @@
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.fr.function;
import com.fr.base.BaseUtils;
import com.fr.base.SynchronizedLiveDataModelUtils;
import com.fr.data.impl.RecursionDataModel;
import com.fr.general.FArray;
import com.fr.general.GeneralUtils;
import com.fr.general.data.DataModel;
import com.fr.invoke.Reflect;
import com.fr.script.AbstractFunction;
import com.fr.script.Calculator;
import com.fr.security.function.RestrictScript;
import com.fr.stable.Primitive;
import com.fr.stable.StringUtils;
import com.fr.stable.exception.FormulaException;
import java.util.Map;
@RestrictScript
public class TREENODEFINDER extends AbstractFunction {
public TREENODEFINDER() {
}
public Object run(Object[] args) throws FormulaException {
if (!this.validateArgs(args)) {
return Primitive.ERROR_NAME;
} else {
boolean noLiveDataModel = !this.liveDataModel4ShareExists();
DataModel dataModel = this.generateDataModel(args);
try {
if (args.length != 2) {
return Primitive.NULL;
}
if (args[0] instanceof FArray) {
FArray nodeArray = (FArray) args[0];
FArray result = new FArray();
for (int i = 0; i < nodeArray.length(); i++) {
result.simpleAdd(this.run(GeneralUtils.objectToString(nodeArray.elementAt(i)), dataModel));
}
return result;
} else {
return this.run(GeneralUtils.objectToString(args[0]), dataModel);
}
} finally {
if (noLiveDataModel && dataModel != null) {
try {
dataModel.release();
} catch (Exception var11) {
this.log(var11.getMessage(), var11);
}
}
}
}
}
private boolean liveDataModel4ShareExists() {
Calculator calculatorProvider = this.getCalculator();
if (calculatorProvider == null) {
return false;
} else {
Map var1 = (Map) calculatorProvider.getAttribute(SynchronizedLiveDataModelUtils.CUR_LIVE_RS);
return var1 != null && !var1.isEmpty();
}
}
public Object run(String nodeValue, DataModel dataModel) {
try {
RecursionDataModel rdm = Reflect.on(dataModel).get("dataModel");
int treeStart = 0;
int m;
for (m = 0; m < rdm.getColumnCount(); ++m) {
String colName = rdm.getColumnName(m);
if (colName.startsWith("FR_GEN_")) {
treeStart = m;
break;
}
}
m = 0;
for (int len = rdm.getRowCount(); m < len; ++m) {
int i = 0;
for (int colCount = rdm.getColumnCount(); i < colCount; ++i) {
String value = GeneralUtils.objectToString(rdm.getValueAt(m, i));
if (StringUtils.equals(nodeValue, value)) {
FArray array = new FArray();
for (int j = treeStart; j < colCount; ++j) {
String val = GeneralUtils.objectToString(rdm.getValueAt(m, j));
if (StringUtils.isNotEmpty(val)) {
array.add(val);
}
}
return array;
}
}
}
} catch (Exception var13) {
this.log(var13.getMessage(), var13);
}
return Primitive.NULL;
}
public boolean validateArgs(Object[] args) {
return args != null && args.length >= 1;
}
public DataModel generateDataModel(Object[] args) {
return BaseUtils.getDataModelFromTableDataName(this.getCalculator(), args[1].toString());
}
public Type getType() {
return REPORT;
}
}
Loading…
Cancel
Save