我有点被这么简单的问题困住了。我正在使用 DynamicReports,如果列值为 null,我想隐藏整行。据我所知,DynamicReports 基于 JasperReports,可以通过选中 TextField 的选项“Remove line when blank”来实现。我怎样才能在 Dynamic 中做到这一点?
我使用的组件:
TextColumnBuilder, ColumnGroupBuilder, JasperReportBuilder
如果我的任何 TextColumns 为空,我想隐藏整行。
最佳答案
好吧,经过一番思考,我发现这个问题可以通过其他方式解决。
我们将使用列、组等属性setPrintWhenExpression(DRIExpression expression)
1. 创建类,它将处理、打印或不打印行。 Dynamic 具有执行此操作的抽象类:
public class ShowExpressionDynamicReports extends AbstractSimpleExpression<Boolean> {
private String fieldName;
public ShowExpressionDynamicReports(String fieldName) {
this.fieldName = fieldName;
}
@Override
public Boolean evaluate(net.sf.dynamicreports.report.definition.ReportParameters reportParameters) {
return reportParameters.getValue(fieldName) != null;
}
}
您应该扩展 AbstractSimpleExpression 以便将其作为参数传递给下面列出的方法。
因此,如果 evaluate(ReportParameters rp)
返回 true,则打印列值。
我还添加了字段 fieldName
,它允许我根据其他列状态打印(或不打印)列。
2.将属性添加到您的
列:
setPrintWhenExpression(DRIExpression 表达式)
组:
.setPrintSubtotalsWhenExpression(DRIExpression 表达式)
或
setFooterPrintWhenExpression(DRIExpression 表达式)
或
setHeaderPrintWhenExpression(DRIExpression 表达式)
取决于你想隐藏什么。
示例:
我们的报告中有 2 列:Product 和 ProductCount 列。如果 ProductCount 为 null(我们没有关于该产品的信息),我想隐藏 Product 列。
因此,为此,我将向 Product 列添加属性 PrintWhenExpression
TextColumnBuilder<String> productColumn = col.column("Product", "Product", type.stringType())
.setPrintWhenExpression(new ShowExpressionDynamicReports("ProductCount"));
关于java - 如果列为空,则 DynamicReports 删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38139845/