How to shade a cell by a class on its column's header & a value in another cell on same row?

How to shade a cell by a class on its column's header & a value in another cell on same row?

Nick HopeNick Hope Posts: 48Questions: 12Answers: 1

Link to test case:
http://live.datatables.net/bufubolo/4/edit

Description of problem:
The linked example above shows a way to shade cells based on a) their class & b) a value in a particular cell in the same row.

$(document).ready( function () {
  var table = $('#example').DataTable( {
    createdRow: function ( row, data, index ) {
      if (data[1] == 'System Architect') {
        $('td.colorbyposition', row).addClass('sysarc'); //Select td by class on td
      }
    }
  } );
} );

Instead of having to add the class colorbyposition to all cells that I want to shade this way, is there a selector I can use that means I only have to add a class (e.g. colbypos in the linked example) to the header (th) of the columns that I wish to shade this way?

In other words, how to select a td by a class on its column's th?

This question has accepted answers - jump to:

Answers

  • silkspinsilkspin Posts: 141Questions: 32Answers: 5
    Answer ✓

    This is how to add a class to a column. It adds the class to the <th> and all the <td> cells.

    https://datatables.net/reference/option/columns.className

  • Nick HopeNick Hope Posts: 48Questions: 12Answers: 1

    Thanks. It's straightforward for me to add the classes in my html when the table is built. I was just thinking that it's "lighter" to only put the class in the header, not each cell.

    To do what I am asking, I suspect I would have to build an array of the indexes of columns that have the class colbypos in the column header, and then reference the values in that array to select the appropriate cells in a row.

    However,.as I am using stateSave: true for remembering the column visibility state, I'm coming to the conclusion that I probably need to add the class on the td anyway and not the th.

  • colincolin Posts: 15,166Questions: 1Answers: 2,588
    Answer ✓

    Yep, the header class won't trickle down onto the column's cells. As silkspin said, columns.className is the way to go,

    Colin

This discussion has been closed.