簡體   English   中英

jQuery求和表行的值

[英]jQuery sum the values of table rows

你好,我有這張桌子 在此輸入圖像描述

我想得到總列jQuery中每行的總和

  //Monthly Marketing Cost Report

         $.get('/dashboard/costs', function(data){
            $.each(data,function(i,value){
                var leads = $('#leads');
                var budget_total_year = $('#full_year_cost');
                var budget_total_month = $('#share_cost');
                var budget_per_lead = $('#cost_per_lead');

                leads.append('<th>' + value.olxTotal + '</th>');
                budget_total_year.append('<th>' + value.budget_total_year + '</th>');
                budget_total_month.append('<th>' + value.budget_total_month + '</th>');
                budget_per_lead.append('<th>' + value.budget_per_lead + '</th>');
            })

        })

HTML

  <tbody id="tableData-marketMonth">
                                        <tr id="leads">
                                            <th>Leads</th>
                                         </tr>
                                         <tr id="full_year_cost">
                                             <th>Full Year Cost</th>
                                         </tr>
                                         <tr id="share_cost">
                                             <th>{{date('F')}} Share of Cost</th>
                                         </tr>
                                         <tr id="cost_per_lead">
                                             <th>Cost per Lead</th>
                                         </tr>
                                    </tbody>

我打算通過php計算總數,但我可以更容易
使用jQuery只需將每一行的總和放在最后謝謝你

嘗試這樣的事情。

$('#tableData-marketMonth tr').each(function () {
    var row = $(this);
    var rowTotal = 0;
    $(this).find('th').each(function () {
        var th = $(this);
        if ($.isNumeric(th.text())) {
            rowTotal += parseFloat(th.text());
        }
    });
    row.find('th:last').text(rowTotal);
});

注意:如果你有td,請將'th'改為'td'。 看着你的jquery,看起來你正在追加。

在循環之前創建變量。 添加到循環中的變量,然后在結尾處分配總和。

 $.get('/dashboard/costs', function(data){

            var sumLeads = 0;
            var sumFullYearCost = 0;
            var sumShareCost = 0;
            var sumCostPerLead = 0;

            var tr_leads = $('#leads');
            var tr_budget_total_year = $('#full_year_cost');
            var tr_budget_total_month = $('#share_cost');
            var tr_budget_per_lead = $('#cost_per_lead');

            $.each(data,function(i,value){


                tr_leads.append('<th>' + value.olxTotal + '</th>');
                tr_budget_total_year.append('<th>' + value.budget_total_year + '</th>');
                tr_budget_total_month.append('<th>' + value.budget_total_month + '</th>');
                tr_budget_per_lead.append('<th>' + value.budget_per_lead + '</th>');

                sumLeads += value.olxTotal;
                sumFullYearCost += value.budget_total_year;
                sumShareCost += value.budget_total_month;
                sumCostPerLead += value.budget_per_lead;
            });


            tr_leads.append('<th>' + sumLeads  + '</th>');
            tr_budget_total_year.append('<th>' + sumFullYearCost  + '</th>');
            tr_budget_total_month.append('<th>' + sumShareCost  + '</th>');
            tr_budget_per_lead.append('<th>' + sumCostPerLead  + '</th>');    
   });

使用Array.mapArray.reduce引導行的示例。 使用jQuery獲取td元素。

var leads = $('#leads');
const total = leads.children('td').toArray().map(x=>Number(x.innerHTML)).reduce((sum, x) => sum + x)
leads.append(`<th>${total}</th>`);

如果你願意,你可以用我的書面代碼進行投票......

HTML

<table>
    <thead>
        <tr>
            <th>MAX ATK</th>
            <th>MAX DEF</th>
            <th>MAX HP</th>
            <th>Overall</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="combat">8170</td>
            <td class="combat">6504</td>
            <td class="combat">6050</td>
            <td class="total-combat"></td>
        </tr>
        <tr>
            <td class="combat">8500</td>
            <td class="combat">10200</td>
            <td class="combat">7650</td>
            <td class="total-combat"></td>
        </tr>
        <tr>
            <td class="combat">9185</td>
            <td class="combat">7515</td>
            <td class="combat">9185</td>
            <td class="total-combat"></td>
        </tr>
    </tbody>
</table>

jQuery的

$(document).ready(function () {
    //iterate through each row in the table
    $('tr').each(function () {
        //the value of sum needs to be reset for each row, so it has to be set inside the row loop
        var sum = 0
        //find the combat elements in the current row and sum it 
        $(this).find('.combat').each(function () {
            var combat = $(this).text();
            if (!isNaN(combat) && combat.length !== 0) {
                sum += parseFloat(combat);
            }
        });
        //set the value of currents rows sum to the total-combat element in the current row
        $('.total-combat', this).html(sum);
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM