Search code examples
javajasper-reportsdynamic-reports

DynamicReports remove row if column is null


I am a little bit stuck with such an easy problem. I am using DynamicReports and I want to hide whole row if column value is null. As I know, DynamicReports is based on JasperReports and it's possible there to do that by checking TextField's option "Remove line when blank". How can I do that in Dynamic?

Components, which I use:

TextColumnBuilder, ColumnGroupBuilder, JasperReportBuilder

I want to hide whole row if any of my TextColumns would be null.


Solution

  • OK, after some thoughts, I found that this problem could be resolved in other way.

    We gonna use column, group etc property setPrintWhenExpression(DRIExpression expression)

    1. Create class, which will handle, print or not to print the row. Dynamic has the abstract class for doing this:

        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;
          }
        }
    

    You should extend AbstractSimpleExpression in order to pass it as parameter to methods which are listed below.

    So, the column value is printed if evaluate(ReportParameters rp) returns true.

    I've also added field fieldName which allows me to print (or not) column due to other's column state.

    2. Add property to your

    column:

    setPrintWhenExpression(DRIExpression expression)

    group:

    .setPrintSubtotalsWhenExpression(DRIExpression expression)

    or

    setFooterPrintWhenExpression(DRIExpression expression)

    or

    setHeaderPrintWhenExpression(DRIExpression expression)

    Depends on what you want to hide.

    Example:

    We have 2 columns in our report: Product and ProductCount columns. I would like to hide Product column if ProductCount is null (we have no information for this Product).

    So, to do that i will add property PrintWhenExpression to Product column

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