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

一个人想着一个人 提交于 2019-12-02 12:02:54

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:

Alas, this is not supported with BIRT.

That's probably not the answer you've hoped for, but it's the truth. This is one of the very few aspects where BIRT is way behind other report generator tools.

However, depending on how you have BIRT integrated into your environment, a workaround approach is possible for PDF export that we use in our solution with great success. The idea is to let BIRT generate a PDF outline based on the grouping. And the BIRT report creates information in the ReportContext about where and how it wants the page numbers to be displayed. After BIRT generated the PDF, a custom PDFPostProcessor uses the PDF outline and the information from the ReportContext to add the page numbers with iText. If this work-around is viable for you, feel free to contact me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!