Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

java - How to restart page number from 1 in different group of BIRT report

Backgroud: Use Java + BIRT to generate report. Generate report in viewer and allow user to choose to export it to different format (pdf, xls, word...).

All program are in "Layout", no program in "Master Page". Have 1 "Data Set". The fields in "Layout" refer to this DS. There is Group in "Layout", gropu by one field. In "Group Header", I create one cell to use as page number. "Page : MyPageNumber". "MyPageNumber" is a field I define which would +1 in Group Header.

Problem: When I use 1st method to generate report, "MyPageNumber" could not show correctly. Because group header only load one time for each group. It would always show 1.

Question: As I know there is "restart page number in group" in Crystal report. How to restart page in BIRT? I want to show data of different group in 1 report file, and the page number start from 1 for each group.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do it with BIRT reports using page variables. For example:

  1. Add 2 page variables... Group_page, Group_name.
  2. Add 1 report variable... Group_total_page.
  3. In the report beforeFactory add the script:

    prevGroupKey = "";
    groupPageNumber = 1;
    reportContext.setGlobalVariable("gGROUP_NAME", "");
    reportContext.setGlobalVariable("gGROUP_PAGE", 1);
    
  4. In the report onPageEnd add the script:

    var groupKey = currGroup; 
    var prevGroupKey = reportContext.getGlobalVariable("gGROUP_NAME");
    var groupPageNumber = reportContext.getGlobalVariable("gGROUP_PAGE");
    if( prevGroupKey == null ){
        prevGroupKey = "";
    }
    
    if (prevGroupKey == groupKey)
    {
     if (groupPageNumber != null)
    {
        groupPageNumber = parseInt(groupPageNumber) + 1;
    }
    else {
        groupPageNumber = 1;
    }
    }
    else {
        groupPageNumber = 1;
        prevGroupKey = groupKey;
    }
    reportContext.setPageVariable("GROUP_NAME", groupKey);
    reportContext.setPageVariable("GROUP_PAGE", groupPageNumber);
    reportContext.setGlobalVariable("gGROUP_NAME", groupKey);
    reportContext.setGlobalVariable("gGROUP_PAGE", groupPageNumber);
    
    var groupTotalPage = reportContext.getPageVariable("GROUP_TOTAL_PAGE");
    if (groupTotalPage == null)
    {
        groupTotalPage = new java.util.HashMap();
        reportContext.setPageVariable("GROUP_TOTAL_PAGE", groupTotalPage);
    }
    groupTotalPage.put(groupKey, groupPageNumber);
    
  5. In a master page onRender script add the following script:

    var totalPage = reportContext.getPageVariable("GROUP_TOTAL_PAGE");
    var groupName = reportContext.getPageVariable("GROUP_NAME");
    if (totalPage != null)
    { 
        this.text = java.lang.Integer.toString(totalPage.get(groupName));
    }
    
  6. In the table group header onCreate event, add the following script, replacing 'COUNTRY' with the name of the column that you are grouping on:

    currGroup = this.getRowData().getColumnValue("COUNTRY");
    
  7. In the master page add a grid to the header or footer and add an autotext variable for Group_page and Group_total_page. Optionally add the page variable for the Group_name as well.

Check out these links for more information about BIRT page variables:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...