【问题标题】:How can I get the sum of a colum for each dynamically generated table?如何获得每个动态生成的表的列的总和?
【发布时间】:2013-11-26 19:26:51
【问题描述】:

我有一个页面,其中包含每个日期动态生成的表格。每个日期都有一列显示每个产品支付的金额。例如,

<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>

所以在这个页面上是一个按日期排序的表格列表,我想显示总金额。

我希望这个总量显示在 td 元素中,值为 class="total"。为此,我尝试使用以下 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 方面的专家,如果有人能给我提供更多见解,我将不胜感激。

【问题讨论】:

  • var $dataRow = $(this).find('tbody tr');$(this).find('.total').html(twoPlacedFloatTotalSum);
  • $(".total").each(function (i) { 必须更改为仅定位一个 total 元素
  • 谢谢我现在明白了! :)

标签: javascript jquery html


【解决方案1】:
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

【讨论】:

  • @user2609980:除了工作答案 (+1),如果需要,我可能会考虑将实际数值和货币作为数据属性添加到 td,即:@987654324 @ 这样您就可以大大简化代码而不必担心货币符号。这也意味着您不必在符号更改时更新解析代码。还可以让您在货币兑换等方面有更大的灵活性。
  • @FrançoisWahl 是的,你是对的......否则格式可能会导致解析值出现问题
  • @FrançoisWahl Aha 听起来是明智之举。该值来自 Razor/C#,如下所示:@Html.DisplayFor(modelItem => order.UnitPrice) 并且模型具有输出 € 的属性。这当然可以更改,然后我执行类似 的操作并给我 5.00 欧元。因此,当我将模型更改为不显示欧元时,这对于拥有一个也可以在非欧元区使用的应用程序来说是一个非常有价值的更新。感谢您的提示!
  • @ArunPJohny 格式化如何导致问题?现在总是显示€。
  • @user2609980:您的模型可以有一个帮助器来返回显示值,而其余部分是分开的。即:@Html.DisplayFor(modelItem =&gt; order.DisplayUnitPrice) &lt;-- should give the sumbol + price&lt;td data-amount="@Html.DisplayFor(modelItem =&gt; order.UnitPrice)"&gt; //&lt;-- should only contain 5.00 now 您的辅助属性可以像这样简单:public class Order{public decimal UnitPrice { get; set; } public string Currency { get; set; } public string DisplayUnitPrice { get { return string.Format("{0} {1}", Currency, UnitPrice.ToString(CultureInfo.InvariantCulture)); }}} 或类似
猜你喜欢
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多