Change cell class based on content

Change cell class based on content

mattkylemattkyle Posts: 7Questions: 2Answers: 0

Hi guys,

I've currently got a table, one column is dedicated to "Owner".
Within each cell on the Owner column, I have wrapped the content in a bootstrap label.

I am essentially looking for the best way to change the background of that label based on the "Owner" found.

I've tried to achieve this using usual jQuery and without the bootstrap label (see below) but it just seems to add the class to every element.

            var owner = $(this).find(".t_owner").html();
            if ($owner = 'Matt Kyle') {
                $('#example .t_owner').addClass('someClass');
            }
<td class="t_owner">  <?php echo $result["t_owner"];?></td>

Answers

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    For modifications to the DOM, rowCallback seems to be the best.

    , "rowCallback": function(row, data, index) {
      var cellValue = data["... name of the data column ..."];
      if (cellValue=="Matt Kyle") {
      $("td:eq(column #)",row).addClass("someclass");
      }
    }
    
  • mattkylemattkyle Posts: 7Questions: 2Answers: 0

    Perfect, that works as expected.

    To complicate it a little further, is it possible to do the same function, but find the contents of the span (within the td I mention above) and apply the the class to that,

    So originally I have:

    <td><?php echo $result["t_owner"];?></td>

    and this works to change the class:

    "rowCallback": function(row, data, index) { var cellValue = data[3]; if (cellValue=="Matt Kyle") { $("td:eq(3)",row).addClass("someclass"); } },

    How do I apply the same logic to:

    <td><span class="label label-default"><?php echo $result["t_owner"];?></span></td>

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    You should just be able to treat cellValue as a jquery object.

    $(cellValue).text()

    Which should return just the text inside the element brackets.

This discussion has been closed.