simonbengtsson/jsPDF-AutoTable

showHead with the value everyPage does not repeat the header of the main table if the nested table has many pages.

cjacobm opened this issue · 3 comments

Hi,

In this sample:

    doc.autoTable({
      head: [{head: 'some head'}],
      body: [''],
      startY: 20,
      showHead: 'everyPage',
      didDrawCell: function (data) {
        if (data.row.index === 0 && data.row.section === 'body') {
          doc.autoTable({
            startY: data.cell.y + 20,
            margin: { left: data.cell.x + 2 },
            tableWidth: data.cell.width - 4,
            styles: {
              maxCellHeight: 4,
            },
            columns: [
              { dataKey: 'id', header: 'ID' },
              { dataKey: 'name', header: 'Name' },
              { dataKey: 'expenses', header: 'Sum' },
            ],
            body: bodyRows(200),
          })
        }
      },
    })

I need the "head" repeat in every page, but this happens only in the first page. 'Some head' is just one record, but its nested table grows for many pages. I need each page had the ´Some head´.

I thought showHead might help, but it didn't work.

employee_data.pdf

mmghv commented

The outer table doesn't know about the inner table, so when the inner table adds a new page, it doesn't inform the outer table about it, for the header to be drawn, the table itself needs to add the new page, this only happen when the table content exceeds the current page.

So you have two options here :

  • Either manually calculate the height of the inner table, then draw the outer table completely after manually setting the outer row height to be the calculated inner table height using minCellHeight, use didDrawCell to get the cell Y and current page, but don't draw the inner table yet, after the outer table is fully drawn, move the doc to the page where the row is at doc.setPage() then draw the inner table using startY and margin to place it inside the row, this will cause it to move to the next page when it needs to break, where the outer table header is already drawn.
  • Or do what you do now but use willDrawPage of inner table to manually draw headers of outer table.

Thank you for your quick response. I tried this outer table/inner table method because my report will have multiple tables, and I am unable to control the header and footer information (Example: How do I print Page 1 of 2 with multiple tables?). So, I would use an outer table to control this information. I will try to use your suggestion. Thanks!

This issue will soon be closed since issues in this project are mainly meant to be bugs or feature requests. Questions are directed to stackoverflow.