Reputation: 73
I want to change the cell color based on a value in the data table. I've tried this code but its not working. Below are the sample code and the Data Table. Any idea & help is much appreciated.
<script type="text/javascript">
var oTable = $('table').DaTable({
'rowCallback': function(row, data, index){
if(data[6]=='Cleared')
{
$(row).find('td:eq(6)').css('color', 'green');
}
else
{
$(row).find('td:eq(6)'.css('color', 'red');
}
}
});
<table class="table table-responsive table-striped table-bordered table-hover table-condensed"
id="edp">
<thead>
<tr>
<th>EDP</th>
<th>Name</th>
<th>Course</th>
<th>Year</th>
<th>Dept.</th>
<th>Clearance</th>
<th>View</th>
</tr>
</thead>
<tbody>
<?php foreach($data as $display)
{
echo "<tr>";
echo "
<td>".$display->EDP."</td>
<td>".$display->Lname.', '.$display->Fname.', '.$display->Mname."</td>
<td>".$display->Course."</td>
<td>".$display->Cyear."</td>
<td>".$display->Dept."</td>
<td>".$display->Status."</td>
<td><a href='".base_url().'office/viewClearance/'.$display->EDP."'><img
class='viewicon' src='".base_url().'assets/icons/view.png'."'></td>
</tr>";
}
?>
</tbody>
Upvotes: 1
Views: 152
Reputation: 2107
Live link https://dotnetfiddle.net/rdL6IU
You can do this @https://datatables.net/examples/basic_init/zero_configuration.html@
$(function () {
$('#example').dataTable({
"createdRow": function (row, data, dataIndex) {
if (data[0] == 'Garrett Winters') {
$(row).addClass('redClass');
}
}
});
})
Upvotes: 1