How to create a Serial Number for total entries?

2019-07-30 04:41发布

Am new to DynamicReportBuilder and want to add a new column to list the serial number of the total rows coming from the db.

Currently, I have researched through ColumnBuilder

but, wasn't able to find a feasible solution. I have tried this for now,

ColumnBuilder serialNo = ColumnBuilder.getNew();
serialNo.setTitle("S No.");
serialNo.setWidth(60);
serialNo.setFixedWidth(true);
logger.info(count+" Total Records");//Count is the total no of rows
for (int j=1;j<count;j++) {
    serialNo.setColumnProperty(j+"",String.class.getName(),j+"");
}
dynamicReportBuilder.addColumn(serialNo.build());

But the problem with this is, it is only showing the last count in the serial number row. Something like this: S. No.

3
3
3
3

1条回答
Lonely孤独者°
2楼-- · 2019-07-30 05:19

If you like to display the row count of your datasource the variabile in jasper report is REPORT_COUNT, you can us a CustomExpression to display this as report is filled.

serialNo.setCustomExpression(new CustomExpression() {
    private static final long serialVersionUID = 1L;

    @Override
    public Object evaluate(Map fields, Map variables, Map parameters) {
        return (Integer) variables.get("REPORT_COUNT");
    }

    @Override
    public String getClassName() {
        return Integer.class.getName();
    }
});

NOTE: Your current code just loops all the rows and changes the propertyName and description of the column, hence the result is serialNo.setColumnProperty((count-1)+"",String.class.getName(),(count-1)+"")

查看更多
登录 后发表回答