How can I dynamically set background to rows After the table has been created?

How can I dynamically set background to rows After the table has been created?

davidzornosadavidzornosa Posts: 4Questions: 4Answers: 0

Each row of the table has an attribute called "id".

I want to have a red background for the row whose "id" is 8.

var selectedId = 8;

table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {               
    if (this.data().id == selectedId) {
        $(table.row(rowIdx).node()).css('background-color', 'red'); // <-  does not work                  
        $(table.row(rowIdx).node()).addClass("redBackgroundClass"); // <-does not work
    }       
});

How can I dynamically add a red background to rows based on their id value?

This question has an accepted answers - jump to answer

Answers

  • nava1021nava1021 Posts: 9Questions: 0Answers: 1
    Answer ✓

    I would recommend using the callback function to paint the rows red. Please have a look.

    function myCallback() {
      var api = $('#table').DataTable();          
      //loop via all rows , check id and apply the css
      api.$('tr').each( function (i) {  
      var _id = $(row).attr('id');
         
         if( _id == 8 )
            $(td).css('background-color','red');
     });
    };
    
This discussion has been closed.