I have a feeling that this is going to be rather simple to answer and that I am missing something rather minor.
So here it goes.
What I have is a table that is being populated based off some mySQL. The datatable code looks like this:
$("#etlTable").DataTable({
"dom": '<"top"iflp<"clear">>rt<"bottom"iflp<"clear">>',
"iDisplayLength": 100,
"ordering": false,
"autowidth": true,
"columns": [{ "data": "file_name","class": "nowrap" },
{ "data": "start_time", "class": "nowrap" },
{ "data": "end_time"},
{ "data": "duration"},
{ "data": "outcome", "class": "chk"},
{ "data": "client_name" },
{ "data": "details" }
],
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
if (aData[4] == "Fail") {
$(nRow).children().each(function (index, td) {
$(this).addClass('res');
});
}
}
});
I think that this might be it seems to be the if statement that is causing the issue. But I am not sure what to do next.
Ideally I would like to highlight the table row when the 'Outcome' column value = "Fail"
I can get it to work without the If Statement in there, but that just hightlights the whole table which is not very helpful to me.
Example of Table row
<tr role="row" class="odd">
<td class=" nowrap">Customer1_File</td>
<td class=" nowrap">2014-10-22</td>
<td>2014-10-22</td>
<td>00:25:26</td>
<td>Fail</td>
<td>Client_name</td>
<td>Job_Code_Details</td>
</tr>
This is what I was using before, but it doesn't work since the table is loaded after this is ran:
<script type="text/javascript">
var i = 0;
var x = document.getElementsByClassName("chk");
while (i <= x.length) {
document.getElementsByClassName("chk")[i].className = "res";
x = document.getElementsByClassName("chk");
};
</script>
If I do it this way:
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
$(nRow).children().each(function (index, td) {
$(this).addClass('res');
});
}
It highlights my whole table.
I am pretty new to JQuery/Javascript ( as in this is my first project, I took it over from someone else and trytingo to piece this thing together and make some improvements. )
So my question is, what I am I doing wrong here? How Can I highlight the row of a table based of the cell value?
Okay so the thing that I was doing wrong was that I was using JSON and trying to access it as an array.
$("#etlTable").DataTable({
"dom": '<"top"iflp<"clear">>rt<"bottom"iflp<"clear">>',
"iDisplayLength": 100,
"ordering": false,
"autowidth": true,
"columns": [{ "data": "file_name","class": "nowrap" },
{ "data": "start_time", "class": "nowrap" },
{ "data": "end_time"},
{ "data": "duration"},
{ "data": "outcome", "class": "chk"},
{ "data": "client_name" },
{ "data": "details" }
],
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
if (aData[4] == "Fail") {
$(nRow).children().each(function (index, td) {
$(this).addClass('res');
});
}
}
});
because it is an array, and they have an alias, I had to do this instead:
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
if (aData['outcome'] == "Fail") {
$(nRow).addClass('highlight');
$(nRow).css('background-color', '#FFFF00');
}
console.log(aData['outcome']);
}
Notice this part here: aData['outcome']
to find this I had to add this: console.log(aData['outcome']);
It now works brilliantly.