Markuos
Markuos

Reputation: 31

jsPDF Autotable change cell color for negative values

I’m using jsPDF Autotable to generate a PDF from an HTML table. The TD element contains an ID that populates the cell with a variable . Everything works fine, but I wanted to set the textColor to red for negative values. I can’t find an example of how to achieve this?

EDIT: I solved making the bottom row of a table red when the values are negative using a hook...

drawCell: function (cell, data) {
    if (summary_balance_weekly <0) {
        if (data.row.index === data.table.rows.length - 1) {
            doc.setTextColor(255,0,0);
        }
    }    
}

Upvotes: 3

Views: 9752

Answers (2)

Megh09
Megh09

Reputation: 61

For the latest version of jspdf-autotable, createdCell function is deprecated, so use didParseCell function. below is the example to change color if data in a cell is negative

didParseCell: function (data) {
    if(data.section === 'body' && data.cell.raw < 0){
        data.cell.styles.textColor = "red";
    }
}

Upvotes: 4

Souvik
Souvik

Reputation: 823

fillColor and textColor both accept RGB array. So the sample code will look like this:

createdCell: function(cell, opts) {
                if (opts.column.index >= 1) { // count startrs from 0
                    // cell.raw contains the cell data
                    cell.styles.fillColor = [216,78,75];     // random color
                    cell.styles.textColor = [58,121,152];
                  }
              }

Upvotes: 1

Related Questions