我有一个.ejs页面,其中包含一个表。在该表中,有4列包含数字。我想在同一页上显示这些列的平均值。
我已经设法得到了平均值,但它非常低效,并且占用了.ejs文件中的大量空间。
这是页面的外观。
下面是我为.ejs文件编写的代码(包括我编写的糟糕的平均值“计算器”):
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<title>MTU Phone Usage Monitor</title>
<link rel="stylesheet" href="/stylesheets/styleTable.css"></link>
</head>
<body>
<div class="banner">
<div class="navbar">
<img src="\images\logo.png" class="logo">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/phone/create">New Entry</a></li>
<li><a href="/table">View Data</a></li>
<li><a href="/help">Help</a></li>
</ul>
</div>
<form action="/phone/find" method="post"></form>
<div class="table-wrapper">
<table class="tableData" id="table">
<caption>
<%= title %>
</caption>
<thead>
<tr>
<th>Name</th>
<th>Education Usage</th>
<th>Shopping Usage</th>
<th>Searching/Browsing usage</th>
<th>Social Media usage</th>
<th>Date and Time</th>
<th></th>
</tr>
</thead>
<tbody>
<% for(var i=0; i < phonelist.length; i++) { %>
<tr>
<td>
<%= phonelist[i].name %>
</td>
<td>
<%= phonelist[i].timeEducation %>
</td>
<td>
<%= phonelist[i].timeShopping %>
</td>
<td>
<%= phonelist[i].timeBrowsing %>
</td>
<td>
<%= phonelist[i].timeSocial %>
</td>
<td>
<%= phonelist[i].createdAt %>
</td>
<td>
<div class="secret">
<form action="/phone/delete" method="post"> <input type="String" value="<%= phonelist[i].id%>" name="id" readonly><button type="Submit">Delete</button></form>
<form action="/phone/update" method="post"> <input type="String" value="<%=phonelist[i].id%>" name="id" readonly><button type="Submit">Update</button></form>
</div>
</td>
</tr>
<% } %>
</div>
<span id="education"></span><br>
<span id="shopping"></span><br>
<span id="browsing"></span><br>
<span id="social"></span>
<script>
var table = document.getElementById("table"),
avgVal, sumVal = 0,
rowCount = table.rows.length - 1; // minus the header
//calculate average education usage
for (var i = 1; i < table.rows.length; i++) {
sumVal = sumVal + parseInt(table.rows[i].cells[1].innerHTML);
}
document.getElementById("education").innerHTML = "Average Education = " + parseFloat(sumVal / rowCount);
avgVal, sumVal = 0;
//calculate average shopping usage
for (var i = 1; i < table.rows.length; i++) {
sumVal = sumVal + parseInt(table.rows[i].cells[2].innerHTML);
}
document.getElementById("shopping").innerHTML = "Average Shopping = " + parseFloat(sumVal / rowCount);
avgVal, sumVal = 0;
//calculate average browsing usage
for (var i = 1; i < table.rows.length; i++) {
sumVal = sumVal + parseInt(table.rows[i].cells[3].innerHTML);
}
document.getElementById("browsing").innerHTML = "Average Shopping = " + parseFloat(sumVal / rowCount);
avgVal, sumVal = 0;
//calculate average browsing usage
for (var i = 1; i < table.rows.length; i++) {
sumVal = sumVal + parseInt(table.rows[i].cells[3].innerHTML);
}
document.getElementById("browsing").innerHTML = "Average Shopping = " + parseFloat(sumVal / rowCount);
avgVal, sumVal = 0;
//calculate average social usage
for (var i = 1; i < table.rows.length; i++) {
sumVal = sumVal + parseInt(table.rows[i].cells[4].innerHTML);
}
document.getElementById("social").innerHTML = "Average Shopping = " + parseFloat(sumVal / rowCount);
</script>
</body>
</html>
下面是数据来源的代码:
phoneRouter.route('/find')
.post((req, res, next) => {
phones.find({ name: req.body.name })
.then((phonetaken) => {
phones.find(req.body)
.then((phonefound) => {
res.render('oneFound.ejs', { 'phonelist': phonefound, title: 'All data recieved from user: ' + req.body.name }); //can use datadisplay
}, (err) => next(err));
})
})
我相信这里的aggregate
方法是用来计算平均值的。我该如何实现它呢?我该如何缩短代码,使其更美观,更可读。我认为我可以在for循环中放入一个for循环,但我不确定如何实现它,并显示所有4个平均值。
这样,我还想在页面底部生成一个平均值的条形图。
如何缩短代码/提高代码效率?如何生成平均值条形图?
2条答案
按热度按时间3htmauhk1#
我已经设法嵌入了2个
for
循环。这样使脚本看起来更干净。查看详情:
然后
span
标签看起来像这样:"现在"它们看起来像这样
u5i3ibmn2#
如果你给予
td
一个类来表示它们所在的列,这就容易多了。然后你可以循环遍历一个与输出span
的id相匹配的类名数组。将值收集到一个数组中,确定平均值并在span
中呈现。第一个