简体   繁体   English

无法计算表格行的总和我从互联网上尝试了很多方法但没有

[英]Can't calculate the sum of table rows I've tried many ways from the internet but nothing

I want to build tables that fetch data from api and I succeeded in that so I need now to calculate numbers of table rows and show total in next row or in span div it does not matter.我想构建从 api 获取数据的表,并且我成功了,所以我现在需要计算表行数并在下一行或 span div 中显示总计,这无关紧要。 I just need total as shown in image..我只需要如图所示的总数..

I already tried many solutions to calculate rows but always get result zero or NaN.我已经尝试了许多计算行的解决方案,但总是得到结果为零或 NaN。 I'm not sure what I'm doing wrong or maybe I just made mistake somewhere in this code... I would appreciate your opinion or solution if possible.我不确定我做错了什么,或者我只是在这段代码的某个地方犯了错误……如果可能的话,我将不胜感激您的意见或解决方案。

在此处输入图像描述

 function statsYesterday() { $.getJSON('https://api.url, function(data) { var data, i, offer, creative, id, conversion, clicks, payout = ""; for (i in data.response.data.data) { offer += "<table><tr><td>" + data.response.data.data[i].Offer.name + "</td></tr></table>"; creative += "<table><tr><td>" + data.response.data.data[i].OfferUrl.name + "</td></tr></table>"; id += "<table><tr><td>" + data.response.data.data[i].Stat.affiliate_info1 + "</td></tr></table>"; conversion += "<table><tr><td>" + data.response.data.data[i].Stat.conversions + "</td></tr></table>"; clicks += "<table><tr><td>" + data.response.data.data[i].Stat.clicks + "</td></tr></table>"; //this code reduce payout by 20% var num = parseFloat(data.response.data.data[i].Stat.payout); var val = num - (num *.20); payout += "<table><tr><td> &euro; " + val + "</td></tr></table>"; }); //end reduce code //this is how code look like without reducing percentage //payout += "<table><tr><td>" + data.response.data.data[i].Stat.payout + "</td></tr></table>"; document.getElementById("offer_name").innerHTML = offer; document.getElementById("creative_name").innerHTML = creative; document.getElementById("id_name").innerHTML = id; document.getElementById("converion_name").innerHTML = conversion; document.getElementById("clicks_name").innerHTML = clicks; document.getElementById("payout_name").innerHTML = payout; }; }); };

 <body> <div class="container"> <br><br><button onclick="statsYesterday()" style="background-color: #4CAF50;border: none;color: white;padding: 16px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;transition-duration: 0.4s;cursor: pointer;">Yesterday</button> &nbsp; &nbsp; &nbsp; <button onclick="statsToday()" style="background-color: #4CAF50;border: none;color: white;padding: 16px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;transition-duration: 0.4s;cursor: pointer;">Today</button> &nbsp; &nbsp; &nbsp; <button onclick="statsLastWeek()" style="background-color: #4CAF50;border: none;color: white;padding: 16px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;transition-duration: 0.4s;cursor: pointer;">Last Week</button> <br><br> <table class="table" id="table"> <thead> <tr> <th>Offer</th> <th>Creative</th> <th>SubID</th> <th>Conversion</th> <th>Clicks</th> <th>Payout</th> </tr> </thead> <tbody> <tr> <td id="offer_name"></td> <td id="creative_name"></td> <td id="id_name"></td> <td id="converion_name"></td> <td id="clicks_name"></td> <td id="payout_name" style="padding-top:30px;"></td> </tr> </tbody> </table> <br><br> </div> </body>

push the values into an array and then you can perform an addition on them later something like this将值推送到数组中,然后您可以稍后对它们执行加法,如下所示

values=[]
//... your code 
 var num = parseFloat(data.response.data.data[i].Stat.payout);
        var val = num - (num * .20);
         values.push(val)

and then later you can simply iterate over the array and add the values然后稍后您可以简单地遍历数组并添加值

 let total=values.reduce((acc,curr)=>acc+curr)
  document.getElementById("total").textContent=total

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用方法 myFunction 返回(名称)我已经尝试了很多方法但它不起作用 - How to invoke the method myFunction to return (name) i've tried many ways but it doesn't work 检测Adblock的可能方法? 我尝试了很多事情 - Possible ways of detecting Adblock? I've tried so many things 为什么不能以短代码将Web Clip图标添加到我的Web应用程序中。 我已经尝试了一切? - Why can't I add a web clip icon to my webapp in dashcode. I've tried everything? 为什么我的 javascript 不能重定向以下代码,我尝试了多种解决方案,我在 StackOverFlow 中找到了 - Why can't my javascript redirect the following code, I've tried multiple solution that i found in StackOverFlow 我已经尝试了createTextNode的每个换行符,但是我不能 - I've tried every line break for createTextNode but I can't make one 如何在不调用键的情况下从对象数组中获取值(我通过互联网搜索找不到与此完全相同/类似的问题) - How to get a value from an Array of object without calling the key (i've searched through the internet can't found the exact/similar problem to this) 即使我尝试了很多方法来强制重绘,进度条也不会在繁重的工作中重绘 - Progress bar wouldn't repaint during heavy work, even though I've tried many methods to force repaint 为什么不能在Internet Explorer中使用JavaScript将行动态添加到HTML表中? - Why can't I dynamically add rows to a HTML table using JavaScript in Internet Explorer? 我想使用位置功能将 class 添加到#cart-total-remove 我尝试了很多方法但没有奏效 - I want to add a class to #cart-total-remove with the location fonction i tried many ways but didn't work 我尝试了一些 ajax 请求,但它不起作用 - I've tried to some the ajax request but it won't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM