javascript - 获取列的总数
问题描述
我创建了带有 json 响应的表。我想得到列的总和
const pipeline= [{Geo: "APAC", SalesStage: "Proposal Submitted", count: 11, pipelinevalue: 1},
{Geo: "NSU", SalesStage: "Proposal Submitted", count: 4, pipelinevalue: 2},
{Geo: "Middle East", SalesStage: "Proposal Submitted", count: 17, pipelinevalue: 1},
{Geo: "US East", SalesStage: "Proposal Submitted", count: 14, pipelinevalue: 1},
{Geo: "Europe", SalesStage: "Proposal Submitted", count: 9, pipelinevalue: 2},
{Geo: "US West", SalesStage: "Proposal Submitted", count: 6, pipelinevalue: 1}];
genetateTable(pipeline,'#geo_summary thead','#geo_summary tbody',pipeval='1');
function genetateTable(pipeline,divHeader,divBody,pipeval){
$.each(pipeline, function (index, item) {
const $tr = $('<tr>');
if(pipeval=='1'){
$tr.append([item.SalesStage, item.count,item.pipelinevalue].map(x => $('<td>').text(x)));
}else{$tr.append([item.SalesStage, item.count].map(x => $('<td>').text(x)));}
$(divBody).append($tr);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="geo_summary"> <thead style="background-color: #00f; color: #fff"> <tr><th style="background-color: #66cdf2; color: #000">Opportunity</th><th style="background-color: #66cdf2; color: #000">Pipeline Count</th><th style="background-color: #66cdf2; color: #000">Pipeline Value</th></tr></thead> <tbody> </tbody><tfooter><tfooter></table>
我想得到 ount 和 pipelinevalue 的总和。
我想追加<tr><td>Total</td><td>61</td><td>8</td></tr>
在 tfooter 中。
解决方案
const pipeline = [{
Geo: "APAC",
SalesStage: "Proposal Submitted",
count: 11,
pipelinevalue: 1
},
{
Geo: "NSU",
SalesStage: "Proposal Submitted",
count: 4,
pipelinevalue: 2
},
{
Geo: "Middle East",
SalesStage: "Proposal Submitted",
count: 17,
pipelinevalue: 1
},
{
Geo: "US East",
SalesStage: "Proposal Submitted",
count: 14,
pipelinevalue: 1
},
{
Geo: "Europe",
SalesStage: "Proposal Submitted",
count: 9,
pipelinevalue: 2
},
{
Geo: "US West",
SalesStage: "Proposal Submitted",
count: 6,
pipelinevalue: 1
}
];
genetateTable(pipeline, '#geo_summary thead', '#geo_summary tbody', pipeval = '1');
function genetateTable(pipeline, divHeader, divBody, pipeval) {
let totalCount = 0;
let totalValue = 0;
$.each(pipeline, function(index, item) {
const $tr = $('<tr>');
totalCount += item.count;
totalValue += item.pipelinevalue;
if (pipeval == '1') {
$tr.append([item.SalesStage, item.count, item.pipelinevalue].map(x => $('<td>').text(x)));
} else {
$tr.append([item.SalesStage, item.count].map(x => $('<td>').text(x)));
}
$(divBody).append($tr);
});
$('#geo_summary').append($(`<tfoot><tr><td>Total</td><td>${totalCount}</td><td>${totalValue}</tr></tfoot>`))
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="geo_summary">
<thead style="background-color: #00f; color: #fff">
<tr>
<th style="background-color: #66cdf2; color: #000">Opportunity</th>
<th style="background-color: #66cdf2; color: #000">Pipeline Count</th>
<th style="background-color: #66cdf2; color: #000">Pipeline Value</th>
</tr>
</thead>
<tbody> </tbody>
<tfooter>
<tfooter>
</table>
推荐阅读
- python - 从块中提取字符串
- sql - SQLite 长臂检查约束?
- android - Google 机器学习套件的最低 Android API 级别
- django - DRF 视图与 Django 视图中的 request.user
- node.js - Outlook Node.js 插件不会将邮件移动到 Outlook 窗口桌面应用程序上的另一个文件夹
- javascript - 无法将对象存储到javascript中的数组中
- c# - 如何等待异步命令进行单元测试?
- angular - 如何使用angular7超链接数据表中的表列值?
- r - 按值中文本的特定顺序对行进行排序
- php - PayPal 支付网关与 REST Api 问题中的实时凭据集成