How do I add a class to a specific column when using the columns.every function?

How do I add a class to a specific column when using the columns.every function?

taylor5042taylor5042 Posts: 21Questions: 6Answers: 0

I have the following code which works when the column 0 is not hidden.
What I am trying to do is check the values in a hidden column 0 and change the class of column 1 based on the text value in column 0.
I am open to doing this another way, like with rows.every if that is easier, but I am not clear on how to accomplish this.

                        table.columns(0).every(function() {
                            this.nodes().to$().each(function() {
                                if ($(this).text() == 'sometext') {
                                } else {
                                    $(this).next('td').addClass('highlight');
                                }
                            });
                        });

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The problem is $(this).next('td'). There is no next element since DataTables has removed the hidden column from the document.

    I think I'd do it like this:

    table.rows().every( function () {
      if ( $( table.cell( this.index(), 0 ).node() ).text() !== 'sometext' ) {
        $( table.cell( this.index(), 1 ).node() ).addClass( 'highlight' );
      }
    } );
    

    Allan

  • taylor5042taylor5042 Posts: 21Questions: 6Answers: 0

    Allan,
    Thanks so much for your help, it works perfect!
    I couldn't grasp how to do that from the forums/documentation.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Is there anything specific that you think would be useful for me to add to the documentation to explain it a bit more?

    It is slightly more convoluted than I would like that case - there will be other ways to do it, but I think that's probably one of the most efficient.

    Allan

This discussion has been closed.