Cell class change based on value

Cell class change based on value

kringelkringel Posts: 16Questions: 9Answers: 0
edited December 2020 in Free community support

Hello. :)

I'm trying to change <td> cell class based on its value.
Database columns

Column that I want to edit.

My code, but nothing changes

    var table = $('#user_details').DataTable( {
  "rowCallback": function( row, data ) {
    if ( data[4] == "Active" ) {
      $('td:eq(4)', row).addClass('positive');
    }
  },
        ajax: 'php/table.user_details.php',
...

My goal is to do as shown here

What am I doing wrong? Thanks in advance!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited December 2020 Answer ✓

    Likely it boils down to this if statement:

    if ( data[4] == "Active" ) {
    

    What value does data[4] have? You can use console.log or the browser's debugger to take a look. data[4]. needs to match your data structure, array or object, and it needs to reference an existing element in the data.

    If this doesn't help then we will need a link to your page or a running test case showing the issue so we can take a look. Or you will need to debug the rowCallback function to determine the problem.

    Kevin

  • kringelkringel Posts: 16Questions: 9Answers: 0

    Thanks, Kevin! That helped :)

    This code works for me

        var table = $('#user_details').DataTable( {
              "rowCallback": function( row, data ) {
                      console.log( data.user_status.toString() );
        if ( data.user_status === "Active" ) {
          $('td:eq(4)', row).addClass('positive');
        } else if ( data.user_status === "Inactive" ) {
          $('td:eq(4)', row).addClass('negative');
        }
      },
            ajax: 'php/table.user_details.php',
    

This discussion has been closed.