报表查询插件小demo。实现了数据自定义,动态列加载。
public class ReportTest extends AbstractReportListDataPlugin {
//用于存放动态列标识
List<String> dynamicColumns = new ArrayList<>();
@Override
public DataSet query(ReportQueryParam reportQueryParam, Object o) throws Throwable {
//列的标识,即报表上对应字段的标识(这里代表有4个字段,其中三个是普通报表列,一个是报表动态列)
List<String> fieldNames = new ArrayList<>();
fieldNames.add("ahkd_textfield");
fieldNames.add("ahkd_textfield1");
fieldNames.add("ahkd_textfield2");
fieldNames.add("ahkd_textfield3");
dynamicColumns.add("ahkd_textfield3");
//保放最终的数据
Collection<Object[]> coll = new ArrayList<>();
//行数据
String[] displayDatum ={"12","22","23","sdf"};
//行数据插入到集合,每次coll.add(),代表加入一行数据到报表里
coll.add(displayDatum);
coll.add(displayDatum);
coll.add(displayDatum);
//声明一个DataType数组
DataType[] columnTypes = new DataType[fieldNames.size()];
//初始化数组
Arrays.fill(columnTypes, DataType.StringType);
//创建报表行源数据
RowMeta rowMeta = RowMetaFactory.createRowMeta(fieldNames.toArray(new String[]{}), columnTypes);
//固定写法咱也不懂
CollectionInput inputs = new CollectionInput(rowMeta, coll);
return Algo.create(this.getClass().getName()).createDataSet(inputs);
}
@Override
public List<AbstractReportColumn> getColumns(List<AbstractReportColumn> columns) throws Throwable {
dynamicColumns.forEach(dynamicColumn->
columns.add(createReportColumn(dynamicColumn, ReportColumn.TYPE_TEXT, "我是动态列"))
);
return columns;
}
/**
* @param fieldKey 列标识
* @param fieldType 字段类型
* @param caption 字段名
* @return kd.bos.entity.report.ReportColumn
*/
private ReportColumn createReportColumn(String fieldKey, String fieldType, String caption) {
ReportColumn reportColumn = new ReportColumn();
reportColumn.setFieldKey(fieldKey);
reportColumn.setFieldType(fieldType);
reportColumn.setWidth(new LocaleString("100px")); //设置宽度
reportColumn.setCaption(new LocaleString(caption));
return reportColumn;
}
}