代码之家  ›  专栏  ›  技术社区  ›  quento

如果列为空,DynamicReports将删除行

  •  2
  • quento  · 技术社区  · 10 年前

    我有点被这样一个简单的问题困住了。我正在使用DynamicReports,如果列值为null,我想隐藏整行。据我所知,DynamicReports是基于JasperReports的,可以通过选中TextField的选项“ 空白时删除行 “.我如何在Dynamic中做到这一点?

    我使用的组件:

    TextColumnBuilder, ColumnGroupBuilder, JasperReportBuilder
    

    如果我的任何TextColumns为空,我想隐藏整行。

    1 回复  |  直到 10 年前
        1
  •  1
  •   quento    9 年前

    好吧,经过思考,我发现这个问题可以用其他方法解决。

    我们将使用列、组等属性 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. 将属性添加到您的

    列:

    组:

    .setPrintSubtotalsWhenExpression(DRIExpression expression)

    setFooterPrintWhenExpression(DRIExpression expression)

    setHeaderPrintWhenExpression(DRIExpression expression)

    取决于你想隐藏什么。

    例子:

    我们在报告中有两列:Product和ProductCount列。如果ProductCount为空(我们没有此产品的信息),我想隐藏Product列。

    为此,我将添加属性 PrintWhenExpression 到“产品”列

    TextColumnBuilder<String> productColumn = col.column("Product", "Product", type.stringType())
    .setPrintWhenExpression(new ShowExpressionDynamicReports("ProductCount"));
    
    推荐文章