2013-08-22 36 views
1

我目前正在使用DynamicJasper開發報表。這是我第一次使用它。我正在學習幾個教程,但是我無法解決這個問題。我有一個分組的列狀態。此列只能有兩個值 - state1和state2。基本上,我很期待是一樣的東西DynamicJasper - 分組列似乎不分組

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

您應該按*州* –

+0

排序數據感謝了。您應該將此作爲答案發布,以便我可以接受並讚揚它,並且還會爲未來會遇到同樣問題的DJ新手提供支持。 – Bnrdo

+0

不客氣:)我已經完成 –

回答

1

我覺得你忘了按狀態字段排序數據。

嘗試排序數據,一切都會好的。


您可以在Data Grouping主題中找到更多關於分組的信息。

-1

您實際上是創建你的bean TheBean對象的列表,這是爲什麼它會像

state1 
branch productline columnItem columnCode 

state2 
branch productline columnItem columnCode 

首先要包含stateTheBean對象豆狀

class TheStateBean { 
    private String state; 
    private TheBean theBean; 

    // Getter and Setter 

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

然後做任何你想要的這個名單做。該列表將顯示您的願望輸出。

+0

感謝您的輸入,但我嘗試過但仍然一樣。 Alex K的答案解決了它。 – Bnrdo