PDF style

PDF style

Rich_WalkerRich_Walker Posts: 9Questions: 3Answers: 0

Is it possible to change the cell background colour if its a negative value or as minus symbol?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Rich_Walker ,

    Yep, take a look at this example here : it changes the age column to be blue if it's under 40!

    Hope that helps,

    Cheers,

    Colin

  • JokrisJokris Posts: 5Questions: 2Answers: 0
    edited April 2018

    @colin How would you modify the code to look through every column in a table
    Or every cell in a defined range, say column 3-8?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited April 2018

    Hi @Jokris,

    To get the range, I'd keep it simple, just have it in a loop, something like:

              for (var column = 3; column <= 8; column++) {
                age = table.column(column).data().toArray();
                for (var i = 0; i < age.length; i++) {
                  if (age[i] < 40) {
                    doc.content[1].table.body[i+1][column].fillColor = 'blue';
                  }
                }
              }
    

    Then, for all columns, just widen that loop.

    Cheers,

    Colin

  • Rich_WalkerRich_Walker Posts: 9Questions: 3Answers: 0

    Hi @colin This was going to be my next question. I have put your snippet in but can't create the pdf. http://live.datatables.net/livabosi/1/edit & haven't got any errors.

    Is it possible to get it to change the footer color aswell?

    Thanks in advance.

    Rich

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi Rich,

    There is a console error, but it is vague - the problem is because you're using the test data, which only has 6 columns, and you're trying to scan 8 of them. That code I sent was more of an example of what could be done, rather than one to use as gospel.

    Cheers,

    Colin

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited April 2018 Answer ✓

    Change

    column <= 8;

    to

    column <= 5;

    to get your PDF from the example. That should explain how to fix your own code

  • JokrisJokris Posts: 5Questions: 2Answers: 0

    @colin or @tangerine
    I am trying to apply it to this table stored in a database which doesn't have any TD tags. I can print the PDF but nothing is colored. Say I want to add background color to all cells on table below below with words 'Accountant or 'Edinburgh.


    Following code is added after extend: 'pdfHtml5', text: 'PDF',

    customize: function(doc) {
      for (var column = 0; column <= column.length; column++) {
        celldata = table.column(column).data().toArray();
      for (var i = 0; i < age.length; i++) {
        if (celldata[i] == 'Accountant' || celldata[i] == 'Edinburgh') {
    doc.content[1].table.body[i+1][column].fillColor = 'blue';
          }
        }
      }
    },
    

    Still cant get it to work :neutral:

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I think you need to just look at that code, and debug it.

    Two obvious things stand out,, that first for loop is suspect:

    for (var column = 0; column <= column.length; column++) {
    

    You're making column an integer, and then testing column.length, so that's definitely wrong.

    And so is the second:

    for (var i = 0; i < age.length; i++) {
    

    You've got the variable age from my example, which doesn't exist in your code.

This discussion has been closed.