DynamicJasper - 分组列似乎没有分组

时间:2013-08-22 03:40:10

标签: java jasper-reports dynamic-jasper

我目前正在使用DynamicJasper开发报告。这是我第一次使用它。我正在学习几个教程,但我无法理解这个问题。我有一个分组的列状态。此列只能有两个值 - state1和state2。基本上,我期待的是像

state1
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode

state2
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode

但目前,我所拥有的是

state1
branch    productline    columnItem    columnCode

state2
branch    productline    columnItem    columnCode

state1
branch    productline    columnItem    columnCode

state2
branch    productline    columnItem    columnCode

...

就像它们根本就没有分组一样。

这是我的可运行代码:

GroupTest.java

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.builders.GroupBuilder;
import ar.com.fdvs.dj.domain.constants.GroupLayout;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.DJGroup;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;

public class GroupTest{

    public DynamicReport buildReport() throws Exception {

        DynamicReportBuilder drb = new DynamicReportBuilder();
        Integer margin = new Integer(20);

        drb.setTitle("November 2006 sales report")
            .setSubtitle("This is a sample report")
            .setDetailHeight(new Integer(15)).setLeftMargin(margin)
            .setRightMargin(margin).setTopMargin(margin).setBottomMargin(margin)
            .setPrintBackgroundOnOddRows(true);


        AbstractColumn columnState = ColumnBuilder.getNew()
                .setColumnProperty("state", String.class.getName())
                .setTitle("State")
                .setWidth(new Integer(85))
                .build();

        AbstractColumn columnBranch = ColumnBuilder.getNew()
                .setColumnProperty("branch", String.class.getName())
                .setTitle("Branch")
                .setWidth(new Integer(85))
                .build();

        AbstractColumn columnaProductLine = ColumnBuilder.getNew()
                .setColumnProperty("productLine", String.class.getName())
                .setTitle("Product Line")
                .setWidth(new Integer(85))
                .build();

        AbstractColumn columnaItem = ColumnBuilder.getNew()
                .setColumnProperty("item", String.class.getName()).setTitle(
                        "Item").setWidth(new Integer(85))
                .build();

        AbstractColumn columnCode = ColumnBuilder.getNew()
                .setColumnProperty("id", Long.class.getName())
                .setTitle("ID")
                .setWidth(new Integer(40))
                .build();

        AbstractColumn columnaQuantity = ColumnBuilder.getNew()
                .setColumnProperty("quantity", Long.class.getName())
                .setTitle("Quantity")
                .setWidth(new Integer(25))
                .build();

        AbstractColumn columnAmount = ColumnBuilder.getNew()
                .setColumnProperty("amount", Float.class.getName())
                .setTitle("Amount")
                .setWidth(new Integer(100))
                .setPattern("$ 0.00")
                .build();

        Style groupLabelStyle = new Style("groupLabel");
        groupLabelStyle.setVerticalAlign(VerticalAlign.BOTTOM);

        GroupBuilder gb1 = new GroupBuilder();
        DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
                        .setGroupLayout(GroupLayout.VALUE_IN_HEADER)
                        .build();

        drb.addColumn(columnState);
        drb.addColumn(columnBranch);
        drb.addColumn(columnaProductLine);
        drb.addColumn(columnaItem);
        drb.addColumn(columnCode);
        drb.addColumn(columnaQuantity);
        drb.addColumn(columnAmount);

        drb.addGroup(g1); 

        drb.setUseFullPageWidth(true);

        DynamicReport dr = drb.build();

        return dr;
    }

    public static void main(String[] args) throws Exception {
        GroupTest test = new GroupTest();                                   
        DynamicReport dr = test.buildReport();                                  
        JRDataSource ds = new JRBeanCollectionDataSource(TheBean.getDummyBeans());      
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);   
        JasperViewer.viewReport(jp);    
    }

}

TheBean.java

import java.util.ArrayList;
import java.util.List;

public class TheBean{

    private String state;
    private String branch;
    private String productLine;
    private String item;
    private Long id;
    private Long quantity;
    private Float amount;

    public TheBean(String state, String branch, String productLine,
            String item, Long id, Long quantity, Float amount) {
        this.state = state;
        this.branch = branch;
        this.productLine = productLine;
        this.item = item;
        this.id = id;
        this.quantity = quantity;
        this.amount = amount;
    }

    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getBranch() {
        return branch;
    }
    public void setBranch(String branch) {
        this.branch = branch;
    }
    public String getProductLine() {
        return productLine;
    }
    public void setProductLine(String productLine) {
        this.productLine = productLine;
    }
    public String getItem() {
        return item;
    }
    public void setItem(String item) {
        this.item = item;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getQuantity() {
        return quantity;
    }
    public void setQuantity(Long quantity) {
        this.quantity = quantity;
    }
    public Float getAmount() {
        return amount;
    }
    public void setAmount(Float amount) {
        this.amount = amount;
    }
    public static List getDummyBeans(){
        List theBeans = new ArrayList();

        for(int i = 0; i < 10; i++){
            String s = "";
            if(i % 2 == 0)
                s = "state1";
            else
                s = "state2";
            TheBean bean = new TheBean(s, "branch", "productLine", "item", new Long(10L), new Long(10L), new Float(10));
            theBeans.add(bean);
        }
        return theBeans;
    }
}

2 个答案:

答案 0 :(得分:1)

我认为您忘记按字段对数据进行排序。

尝试对数据进行排序,一切都会好的。


您可以在Data Grouping主题中找到有关分组的更多信息。

答案 1 :(得分:-1)

您实际上正在创建一个bean TheBean对象的列表,这就是它的原因

state1
branch    productline    columnItem    columnCode

state2
branch    productline    columnItem    columnCode

首先制作一个包含stateTheBean对象的bean,如

class TheStateBean {
    private String state;
    private TheBean theBean;

    // Getter and Setter

    //Write your code to populate the list of TheStateBean object
}

然后对此列表执行任何操作。此列表将显示您的愿望输出。