Skip to content Skip to sidebar Skip to footer

Jquery Count Tr With Some Condition In Td

I have a table with 6+ table rows. Each tr contains 6 td's. Each td has a data-name attribute of either: completed or not completed. I need to check which tr's have 5 td's with the

Solution 1:

You can use filter to get the number of td elements in each row with the completed values. Try this:

$('#workflowviz tr').each(function() {
    var $tr = $(this);
    var $tds = $tr.find('td').filter(function() {
        return $(this).data('name') == 'stage: completed ';
    });
    if ($tds.length == 5) {
        $tr.addClass('green');
    }
});

Example fiddle

You would then just need to setup the .green class in your CSS to make the background of that particular tr element green.

Solution 2:

I have created a jsFfiddle for you using your code..

Code:-

$(document).ready(function() {
     $("table tr").each(function() {
         if ($(this).find("td[data-name='stage: completed ']").length == 5)
             $(this).addClass("green")
     })
 });

and its performance is also good..

working example:- http://jsfiddle.net/BtkCf/172/

to see the its performance see this link and open console see time taken by this code. http://jsfiddle.net/BtkCf/173/

thanks

Post a Comment for "Jquery Count Tr With Some Condition In Td"