pdfHtml5 how to check every cell for a class and align

pdfHtml5 how to check every cell for a class and align

itajackassitajackass Posts: 165Questions: 49Answers: 3

Hi I need to export a table in excel and PDF.

I want:
- 1) remove some pieces of text not needed in exports (done cleaning every cell removing tags with .noExport class)
- 2) export in excel applying some background colors and styles (ok done)
- 3) export in pdf and do align right only for "valuta" and "age" classes (TD)

But i can't do point 3.

The text, in pdf, for those cells (body and footer) are not aligned to right. I can't do it. Anyone could help me?

This is my example: https://live.datatables.net/keqicuvo/1/edit

Tried this code without success.

Also, with some console.log debug I see that - if I hide some columns with COLVIS button, indexed "I" and "K" in table.row(i).column(k) are not the same index in doc.content[1].table.body[i][k] ... so i need to fix also this.

customize: function(doc) {

        var rows = doc.content[1].table.body.length;  // ALLINEA A DX GLI IMPORTi

        for (var i = 0; i < rows; i++) {

                var colonne = doc.content[1].table.body[i].length;

                for (var k = 0; k < colonne; k++) {

                        if (
                                                           $(table.row(i).column(k).nodes()).hasClass("valuta") ||
                                                           $(table.row(i).column(k).nodes()).hasClass("age")
                                                      ) {

                            doc.content[1].table.body[i][k].alignment = 'right';

                        }

                    }

        }

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited November 18

    sorry, my answer referred to xlsx export. Hence I deleted it.

  • itajackassitajackass Posts: 165Questions: 49Answers: 3

    No problem.

    Any idea how to simply loop cells from a row(index) and sync "indexes" from datatable-html-table and pdf-table?

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    The problem is not with how you are applying the alignment to the PDF cell but how you are using the API. Using table.row(i).column(k).nodes() doesn't return what you are expecting. It sill first return the row with in instance of the API allowing column() to work. column().nodes() returns all the nodes in the column, not just the cell you are expecting.

    In your test case you can see that the Salary column is right aligned but no the age column. I haven't debugged the code to determine why it finds the class valuta in the if statement.

    I haven't sat down and taken the time to determine what to change. Do you need to look at the header classes? The reason I ask is doc.content[1].table.body[0] contains the header not the table rows. So you will need to keep track of the header rows versus the table rows.

    If the header is not of concern then I would look at using rows().every() to loop the rows then cells().every() to loop the cells within the row.

    Kevin

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    Answer ✓

    I had some time to workout an example:
    https://live.datatables.net/zixexemu/1/edit

    You may also need to use columns().header() and columns.footer() to apply the same alignment to the header and footer of the PDF.

    Kevin

  • itajackassitajackass Posts: 165Questions: 49Answers: 3

    Thank you for your explanation and example! Very clear implementation! THANKS!!!

Sign In or Register to comment.