2

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?

6 Answers 6

2

You have a typo in the first column definition, but I suspect that's only in your example code above rather than your real code, otherwise you would have noticed.

Try this for your row callback:

"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
    if (aData[4] == "Fail") {
        $(nRow).addClass('res');
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

wait typo? Not sure that I see it... but i've been looking at it for like 8 hours straight now. I tried that logic, still nothing happening.
Huh, the typo is gone now - did you fix it with your edit? It was missing the opening quote and 'class' when I saw it before. Try using Firebug or similar, and put a breakpoint on the if statement - then you can examine aData to see why it's not working.
+1 Yes, you are right - but declaring the CSS "correct" is essential too.
@davidkonrad what do you mean declaring the CSS correct? I tried this code and it did not work for me.
@MitchellHamann see my answer above and the fiddle (latest answer to this question I guess)
1

I can see you are using dataTables 1.10.x. In this version, it is important to declare the CSS "correct" (so it works with the built in CSS being injected) like this :

table.dataTable tr.highlight {
    background-color: lime; 
}

and then declare the fnRowCallBack like this (example) :

var table = $('#example').DataTable({
    fnRowCallback: function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
       if (aData[3] == "Fail") {
           $(nRow).addClass('highlight');
       }
    }    
});

see demo -> http://jsfiddle.net/wqbd6qeL/ ...1.10.x on a paginated table.


Edit : I see it is almost identical to @John-NotANumber's answer, except for the CSS.

6 Comments

So i took a look at that one and I can see it working, but when I try to replicate it for myself I get nothing. jsfiddle.net/SirCaptainMitch/y0fjt9a6
@MitchellHamann, here is your fiddle forked -> jsfiddle.net/d71opdrb You have a lot of issues, could imagine this really is a project you have been forced to take over. seems like a mixup of different things :) 1) it is table.dataTable in the CCS 2) If you have a init function you should call it .. initTableInbound() 3) if you are not having JSON / using ajax as source, columns will not work 4) you should not remake the <table>, $("#etlTable").html(... even if you reinitialize the dataTable, it is not nessecary 5) you didnt had any jQuery or dataTables as externl resources :)
I think that this might be the area of confusion then, because I am using JSON / AJAX as the source. The information comes from mySQL Queries. So I think I do need the columns there and that is what is causing the issues?
@MitchellHamann, so use column as in your OP, it is just not easy to reproduce in a fiddle - I could have used a static JSON, but thought the principle was pretty clear. Do exactly what you do in your question, and use my answer / "John - Not A Number"'s answer, just remember the CSS above.
Thank you for the help, the answer was there the whole time, I just needed to change the 4 to the alias of the array, 'Outcome' and it worked great.
|
1

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.

Comments

1
function hightlight_interseptions(a , b) {
    var count=0;
    $('#ItemTable tr').filter(':not(:first)').each(function() {
        if (count==a || count==b) $(this).addClass('highlight');
        count++;
    }); 
}

Comments

0

Maybe this:

$("tr").each(function () {
    if($(this).find('td').eq(4) == "Fail") {
        $(this).removeClass('chk');
        $(this).addClass('res');
    }
});

2 Comments

This would never work on a paginated table, as far as I can see. Perhaps it will style some rows on the first page, but not when you are changing pages.
It should be used inside the pagination function or using delegate.
0

http://jsfiddle.net/t4rLk1an/2/

alter the table like this:

<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 class="correct">Fail</td>
    <td>Client_name</td>
    <td>Job_Code_Details</td>
</tr>

and jQuery like:

$(document).ready(function(){
    $('.correct').each(function() {          
    if ($(this).html() == 'Fail') {
    $(this).closest('tr').removeClass("chk");
    $(this).closest('tr').addClass("res");
  } 
});
}

);

I'm not sure about your class, as there is no css. To try you can change it to

$(document).ready(function(){
    $('.correct').each(function() {          
    if ($(this).html() == 'Fail') {
    $(this).closest('tr').css("background","red");

  } 
});
}

);

9 Comments

Does that only happen on click? This needs to happen once the datatable has been populated, and for every row in the table that has an Outcome of "Fail" the whole row needs to be highlighted.
Ah sorry, didn't read your question properly. I'll try do a fiddle for you
I will give this a try today. I did something similar to this before but it failed because the data tables weren't initialized and populated for it to be worked on. Which is why I went with the fnRowCallback.
I had a similar issue with datatables, what worked for me (I don't understand why but it did) was to put the code I wrote above before the datatables script.
As with @ADASein, It is hard to see how this should work with a paginated table, or dataTables in general. But surely on a regular table, I guess it would work. It is not recommended to try manipulating the dataTables behaviour / content outside dataTables API / functions, many things can go wrong or will eventually go broken.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.