【问题标题】:jquery get value of table cells in column, add values together and output on pagejquery获取列中表格单元格的值,将值相加并在页面上输出
【发布时间】:2012-06-12 17:54:05
【问题描述】:

这似乎是一个奇怪的问题,我不确定它是否可能。

我在 wordpress 中创建了一个 HTML 表格,并构建了一个 PHP 循环以表格格式输出帖子数据。

我正在使用这个jQuery table sorted plugin 使它成为一个可排序的表。

但我的问题是,是否可以使用 jQuery 将我的表 <th>Hours</th> 列中的所有数值相加并输出结果?

如果需要将一个类添加到<td><?php echo get_post_meta($post->ID, 'Labour Time Hours', true); ?></td> 单元格,那很酷,它不会影响表格排序器。

然后我想在我的表格页脚单元格中输出结果。

这可能吗?我似乎在谷歌上找不到任何东西,但我可能搜索了错误的术语。

谢谢

我的 WORDPRESS 循环生成表内容

<?php 

        $data = new WP_Query(array(

        'post_type'         => 'job',
        'order'             => 'ASC',
        'posts_per_page'    => -1,
        'orderby'           => 'date'

)); ?>

<?php if ($data->have_posts()) : ?>

<table id="myTable" class="tablesorter"> 

    <thead> 

        <tr> 

            <th>Date</th> 
            <th>Job Title</th> 
            <th>Category</th> 
            <th>Hours</th> 

        </tr> 

    </thead> 

    <tbody> 

        <?php while ($data->have_posts()) : $data->the_post(); ?>

        <tr> 

            <td><?php the_time('Y/m/d'); ?></td> 
            <td><a href="<?php the_permalink() ?>" title="View Job" ><?php the_title(); ?></a></td> 
            <td><?php the_category(', ') ?></td> 
            <td><?php echo get_post_meta($post->ID, 'Labour Time Hours', true); ?></td> 

        </tr> 

        <?php endwhile; ?>

    </tbody>

    <tfoot>

        <tr>

            <td></td>
            <td></td>
            <td></td>
            <td><!-- Total to go here --></td>

        </tr>

    </tfoot>

</table>

<?php unset($data); endif; wp_reset_query(); ?>

【问题讨论】:

    标签: jquery add


    【解决方案1】:

    一个简单的方法是为要添加的元素分配一个类。

    所以你会做这样的事情:

    <td class="add">5</td>
    <td class="add">8</td>
    <td id="Total"></td>
    

    然后,如果您绝对肯定这些值将始终是整数(或浮点数),您可以这样做

    function adding() {
        var total = 0;
        $(".add").each(function(index, element) {
            total += parseInt($(element).text());
        });
        $("#Total").text(total);
    }
    

    【讨论】:

    • 谢谢,这似乎是非常直接的方法,我可以看到脚本的作用 - 但奇怪的是什么都没有输出?我会做一个小提琴然后回复你。
    【解决方案2】:

    你可以用一些相当简单的 jQuery 来做到这一点:

    var $table = $('#myTable'),
        $tr = $table.find('tbody tr'),
        totalHours = 0;
    
    $tr.each(function(i, el){
        totalHours += parseInt($(this).find('td:last').html() || 0, 10);
    });
    
    $table.find('tfoot td:last').html(totalHours);
    

    See demo

    【讨论】:

    • 这看起来很棒,应该是它的直接副本和过去,它将在我的循环中工作。然而,我只是得到NaN 结果。我将创建一个小提琴来复制我的桌子并返回给你。谢谢
    • NaN 的一个可能原因可能是您正在解析的值之一是空字符串。您可以通过var hours = parseInt($(this).find('td:last').html(), 10); totalHours += isNaN(hours) ? 0 : hours; 处理
    • 更新了针对该用例的修复程序。
    【解决方案3】:

    最好在服务器上添加imo,但你可以这样做:

    var total = 0;
    
     $("td.whateverClass").each(function () 
        { 
            total += $(this).text();
    
        }
    

    【讨论】:

    • 谢谢。真的看不到这里发生了什么,我的结果是 var total,我应该能够使用 total var 将它放入我的总单元格吗?
    【解决方案4】:

    将hours 类添加到您各自的表格单元格&lt;td class='hours&gt;&lt;/td&gt;'

    然后你就可以这样 jQuery 了。

    var sum = 0;

    然后

    $('tbody td.hours').each(function(index, td)) { sum += parseInt(td.html()); });

    最后

    $('tfoot td.hours').html(sum);

    【讨论】:

    • 谢谢,你似乎得到了一个解析错误 - 说额外的令牌,但没有。 ..index, td)) 之后似乎有一个额外的 ) 但如果我删除它,它仍然会奇怪地抛出一个错误。我会用小提琴回复你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2014-11-28
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多