how to get the subgroup page numbers



  • I reviewed the the playground sample at https://playground.jsreport.net/w/admin/Wy6stA8t about the merging and header and footer and pdf-utils about the pdfCreatePagesGroup, but i cannot determine how to get the page number of subgroups. Let say I have templates like this

    {{#each students}}
        <h1 style='page-break-before: always'>{{name}}</h1>    
        {{{pdfCreatePagesGroup name}}}
        <div>lots of other content expanding to multiple pages</div>
        ....
    {{/each}}
    

    And as you can see single student spans multiple page, so i want a paging like Student "James Doe" - Page 1 of 3, then Student "Anna Capri" - Page 1 of 3

    It's a total of 6 pages, but I want the paging respects the paging of groups.



  • You get on the merged operation data input like this

    {
        ...yourDataOnInup,
       $pages: [{
         group: 'James Doe'
       }, {
         group: 'James Doe'
       }, {
         group: 'James Doe'
       }, {
         group: 'Anna Capri'
       }, {
         group: 'Anna Capri'
       }, {
         group: 'Anna Capri'
       } ]
    }
    

    Now you do the pages iteration and header print. On every header you know the page index. You can iterate from the index up to find out the group start and down to find out the group end. This way you can calculate the page index and pages count with respect to the group.

    function getPageInfo (pages, index) {
        const group = pages[index].group
        
        let gstart = index
        while (gstart - 1 > -1 && pages[gstart - 1].group === group) {
            gstart--
        }
    
        let gend = index
        while ((gend + 1 < pages.length) && pages[gend + 1].group === group) {
            gend++
        }
               
        return (index - gstart + 1) + ' of ' + (gend - gstart + 1)
    }
    

    https://playground.jsreport.net/w/anon/AJda9jIU



  • Thanks i will try it our asap



  • Yep it works.. thanks, these needs a blog or recipe entry please


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.