如何获得每个动态生成的表的列的总和
How can I get the sum of a colum for each dynamically generated table?
我有一个页面,其中包含每个日期动态生成的表。每个日期都有一个列,显示每个产品的支付金额。例如,
<table class="table">
<thead>
<tr>
<th>
<b>Product</b>
</th>
<th>
<b>Amount</b>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Kroket
</td>
<td>
€ 5.00 <!-- Dynamically generated -->
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total:</td>
<td class="total"></td>
</tr>
</tfoot>
</table>
因此,在这个页面上是一个按日期排序的表格列表,我想显示其总额。
我希望这个总额显示在class="total"的td元素中。为此,我尝试使用以下JQuery函数:
function updateTotalOfTotals() {
$(".table").each(function () {
var totalSum = 0;
var $dataRow = $('table tbody tr');
$dataRow.each(function () {
// In reality I have some more td elements and the amounts
// are in the 4th column.
$(this).find('td:nth-child(4)').each(function (i) {
totalSum += parseFloat($(this).text().replace("€", ""));
});
});
var twoPlacedFloatTotalSum = parseFloat(totalSum).toFixed(2);
$(".total").each(function (i) {
$(this).html(twoPlacedFloatTotalSum);
});
});
}
当我只有一张表时,类似的函数也会起作用。但在这里,我无法让它工作,因为它显示了每个表中所有表的总数。无论如何,我都不是JavaScript/JQuery方面的专家,如果有人能为我提供更多见解,我将不胜感激。
function updateTotalOfTotals() {
$(".table").each(function () {
var totalSum = 0;
//target only the trs from current table
var $dataRow = $(this).find('tbody tr');
$dataRow.each(function () {
$(this).find('td:nth-child(2)').each(function (i) {
totalSum += parseFloat($(this).text().replace("€", ""));
});
});
var twoPlacedFloatTotalSum = parseFloat(totalSum).toFixed(2);
//target only the total from current table
$(this).find(".total").html(twoPlacedFloatTotalSum);
});
}
演示:Fiddle
相关文章:
- 如何获得一个动态增长的自定义对象的Javascript数组
- 如何获得每个动态生成的表的列的总和
- 如何根据高图中的值获得动态颜色变化
- 如何在 JavaScript 中动态创建的行中获得 2 列的差异
- 如何获得动态 JQuery 移动手风琴.
- 动态更新 url/html 标题时,如何获得类似 facebook 的按钮来注册更改?(不是 fb:like 中的 hr
- AngularJS href 没有获得动态值
- 将 id 从 foreach 循环动态传递到仅申请一行的锚点标签到 jquery,我无法获得其他行的弹出窗口
- 如何获得<text区域>使用jQuery动态获取值
- HTML客户端无法获得动态添加的数据
- 如何在React上获得动态引用
- 从PHP:如何获得动态生成的javascript图像
- 按字母顺序从复选框组合中获得动态结果
- 如何在SVG中获得动态javascript引用
- 如何在ipad上获得动态插入视频的尺寸?JavaScript
- 我如何在javascript中获得动态时间选择器值
- AS3:我如何从事件监听器函数中的LoaderInfo获得动态加载器URL
- 如何在jquery函数中获得动态添加的特定id ?
- 如何在"dumb"上获得动态(部分在被选择时显示)表单WAP电话(没有Javascript)
- 我如何通过java从http web请求获得动态内容