简体   繁体   English

JQuery用于包含HTML表数据的每个循环

[英]JQuery For each loop with HTML table data

So I have an HTML table that looks like the following. 所以我有一个HTML表,如下所示。

<div class="timecard">
 <h3>tommytest</h3>

<table class="misc_items timecard_list" border="0" cellpadding="2" cellspacing="0" style="margin:0 auto;">
    <tbody>
        <tr class="display_row odd">
            <td align="left" class="job_code" style="color:#000099">2400-Orchard</td>
            <td align="right">9:47am</td>
            <td align="right">5/19/2014</td>
            <td align="right" class="hrs">01:00</td>
        </tr>
        <tr class="display_odd row">
            <td align="left" class="job_code" style="color:#000099">1200-Duffy's</td>
            <td align="right">12:37am</td>
            <td align="right">5/17/2014</td>
            <td align="right" class="hrs">2:00</td>
        </tr>
    </tbody>
</table>
</div>

<div class="timecard">
 <h3>testtest</h3>

<table class="misc_items timecard_list" border="0" cellpadding="2" cellspacing="0" style="margin:0 auto;">
    <tbody>
        <tr class="display_row odd">
            <td align="left" class="job_code" style="color:#000099">2400-Orchard</td>
            <td align="right">9:47am</td>
            <td align="right">5/19/2014</td>
            <td align="right" class="hrs">01:00</td>
        </tr>
        <tr class="display_odd row">
            <td align="left" class="job_code" style="color:#000099">1200-Duffy's</td>
            <td align="right">12:37am</td>
            <td align="right">5/17/2014</td>
            <td align="right" class="hrs">2:00</td>
        </tr>
    </tbody>
</table>
</div>
<div id="total"></div>

Then I have a jquery script that takes the total "job_code" hours and adds them up for each individual one. 然后我有一个jquery脚本,它占用了总的“job_code”小时数并为每个单独的一个添加它们。 However, right now the script combines "tommytest" and "testtest" job codes together. 但是,现在该脚本将“tommytest”和“testtest”作业代码组合在一起。 I'm trying to get it to calculate each one individually and print it underneath each's respected table. 我试图让它单独计算每一个并将其打印在每个受尊重的表格下面。 Any ideas are greatly appreciated. 任何想法都非常感谢。

$(document).ready(function () {

var timeString = $(this).next('td.hrs').text();
var components = timeString.split(':');
var seconds = components[1] ? parseInt(components[1], 10) : 0;
var hrs = parseInt(components[0], 10) + seconds / 60;
total += hrs;

var temp = [];
$('.job_code').each(function (index, element) {
    var text = $(this).text();
    if (text != 'Out') {
        temp.push(text);
    }
});

// remove duplicates
var job_code = [];
$.each(temp, function (index, element) {
    if ($.inArray(element, job_code) === -1) job_code.push(element);
});

var sum = {};
$.each(job_code, function (index, element) {
    var total = 0;
    $('.job_code:contains(' + element + ')').each(function (key, value) {
        var timeString = $(this).siblings('td.hrs').text();
        var components = timeString.split(':');
        var seconds = components[1] ? parseInt(components[1], 10) : 0;
        var hrs = parseInt(components[0], 10) + seconds / 60;
        total += hrs;
        sum[index] = {
            'job_code': element,
                'total': total
        };
    });
});

console.log(sum);

$.each(sum, function (index, element) {
    $('#total').append('<p>Total for ' + element.job_code + ': ' + element.total + '</p>');
});

});

Link to jsFiddle: http://jsfiddle.net/Ha546/2/ 链接到jsFiddle: http//jsfiddle.net/Ha546/2/

If at all possible I'd like this to be dynamic as there will be more than just these two tables. 如果可能的话,我希望这是动态的,因为不仅仅是这两个表。 Thanks ahead of time for the help. 提前谢谢你的帮助。

When you query for the TD tags, $(this).next('td.hrs') you are not being specific enough about which table you want the TD tags from. 当你查询TD标签时, $(this).next('td.hrs')你对于你想要TD标签的表格不够具体。 If you are more specific, for example, adding an id to the tables, 如果您更具体,例如,在表中添加id,

<table id="tommytest">...</table>
<table id="testtest">...</table>

then you can query by table like so: 然后你可以像这样查询表:

var tdsFromTommytest = $(this).next('#tommytest td.hrs')
var tdsFromTesttest = $(this).next('#testtest td.hrs')

Now that you have the two separate TD lists, you can process how you want. 现在您有了两个单独的TD列表,您可以按照自己的方式进行处理。 That, I think is the crux of your problem. 我认为这是你问题的症结所在。 But hopefully that helps you enough to see how you'd update a separate total tag under each table. 但希望这足以帮助您了解如何在每个表下更新单独的总标记。

You could do this for tommyTest and subsequently for testtest : 您可以为tommyTest执行此操作,然后进行testtest:

Add id to the individual table 将id添加到单个表中

<table id="tommyTest">

And in javascript create different method for calculating the value : 并在javascript中创建不同的计算值的方法:

$('#tommyTest').find('.job_code').each(function (index, element) {
        var text = $(this).text();
        if (text != 'Out') {
            temp.push(text);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM