1

So I have an HTML table displaying the results from a query. A sample of the table is as follows:

<div class="timecard">
<tr class = "display-even-row">
    <td align="left" style="color:#000099">2400-Duffy's</td>
    <td align="left" class="hrs">4</td>
<tr class = "display-odd-row">
    <td align="left" style="color:#009900">1500-Orchard</td>
    <td align="left" class="hrs">2</td>
</div>

I want to total the "hrs" class separately for the 2400-Duffy's records and the 1500-orchard records. The following is the JQuery statement I am trying to use.

var sum=0

$(".hrs").each(function(i) {
    if($('style') == 'color#000099') {
        sum = sum + parseInt($(this).text());
    }else if ($('style') == 'color#009900' {
        sum2 = sum + parseInt($(this).text());
});

$(".total").append("Total Duffy hours: " + sum);
$(".total").append("Total Orchard hours: " + sum2);

However, the above does not work properly and honestly I'm trying to automate it more as it has more that one "job code" than just the two listed above. Any suggestions would be great. thanks.

0

3 Answers 3

1

If you want a more automated method, so create it !

$.fn.sum = function() {
    var sum=0;
    $(this).each(function() {
        sum += Number($(this).text().trim());
    });

    return sum;
}

Now use your function. Use class instead of style. For example duffy class must make the color not the style. write your arbitary selector and calculate sum.

var sum = $(".duffy .hours").sum();
Sign up to request clarification or add additional context in comments.

Comments

0

see this http://jsfiddle.net/MnJ35/1/ and use this code

var sum=0

$(".hrs").each(function(i) {
    if($(this).prev('td').css('color')=='rgb(0, 153, 0)') {
    sum = sum + parseInt($(this).text());
    }
});

$(".total").append("Total Duffy hours: " + sum);

1 Comment

Your suggestion does not include the other option seen above. This is important as the whole HTML table has multiple rows of those that will need to be totaled together so that if there are three Duffy's rows they are all added to show Total Duffy Hours = 6. Thanks.
0
Your html code is not valid .because of there is not table tag . 

Working Demo

html Code

<div class="timecard">
    <table>
<tr class = "display-even-row">
    <td align="left" style="color:#000099">2400-Duffy's</td>
    <td align="left" class="hrs">4</td>
      </tr>
<tr class = "display-odd-row">
    <td align="left" style="color:#009900">1500-Orchard</td>
    <td align="left" class="hrs">2</td>
    </tr>
    </table>
</div>

JS

    var sum=0;
    var sum2=0;
    $(".hrs").each(function(i){

        var rgb=$(this).prev("td").css("color");
        var hxColor=hexc(rgb);

        if (hxColor == '#000099'){

            sum = sum + parseInt($(this).text())
        }else if (hxColor == '#009900' ){
            sum2 = sum2 + parseInt($(this).text());

        }

});

function hexc(colorval) {
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i]).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    color = '#' + parts.join('');

    return color;
}

1 Comment

Thank you for your input. However, your code does not total up job codes if say another 4 hours at 1500-Orchard is added in another <td>. Is this possible?

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.