首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jQuery对表行的值求和

jQuery对表行的值求和
EN

Stack Overflow用户
提问于 2019-03-21 03:35:59
回答 3查看 5.7K关注 0票数 1

你好,我有一张桌子

我想要获取合计列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只需将每行的和放在末尾非常感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-03-21 03:50:44

在循环之前创建变量。向循环中的变量相加,然后在最后赋值总和。

 $.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>');    
   });
票数 1
EN

Stack Overflow用户

发布于 2019-03-21 03:47:19

试试这样的东西。

$('#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,它看起来像是在附加th的。

票数 1
EN

Stack Overflow用户

发布于 2019-03-21 03:58:49

使用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>`);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55268900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档