模板引擎将我的代码设置在错误的位置,直到表标题为止。它成功设置了简单的字符串对象,但是将我的表行设置为绝对错误。
app.js
app.get("/", function(req,res) {
const collection = req.app.locals.collection;
collection.find({}).toArray(function(err, messages) {
participants = Analise(messages, collection);
console.log(participants);
if(err) return console.log(colors.red(err));
res.render("main.hbs", {
title: "tg:analitycs",
captition: "Таблица пользователей",
cap: "",
table: genTable(participants),
});
});
});
function genTable(p) {
let rows = '';
for (let i=0; i<p.length; i++) {
rows += "<tr><th>"+p[i].id+"</th><th>"+p[i].first_name+"</th><th>"+p[i].last_name+"</th><th>"+p[i].username+"</th><th>"+p[i].total+"</th><th>"+p[i].type_text+"</th><th>"+p[i].type_image+"</th></tr>";
}
return rows;
}
main.hbs
<body>
{{> header}}
<div class="container">
<div class="test_container">
<ul>
<li>{{captition}}</li>
<li>{{cap}}</li>
</ul>
<table>
<tr>
<th>user_id</th>
<th>First name</th>
<th>Last name</th>
<th>Username</th>
<th>total messages</th>
<th>text</th>
<th>images</th>
</tr>
{{table}} <!---------- MUST BE HERE ----------->
</table>
</div>
</div>
{{> footer}}
</body>
通过以下链接输出屏幕截图:https://jonarhipov.neocities.org/screenshots/Screenshot%202019-06-26%20at%2015.57.57.png
哪里有错误,还是错误?谢谢!
P.S .:呈现表格的最佳方法是什么?我想要:
我不想添加行,删除行,更改行。仅输出表。
答案 0 :(得分:0)
首先,将collection中的所有数据发送到html页面中,如:
res.render("main.hbs", {
title: "tg:analitycs",
captition: "Таблица пользователей",
cap: "",
user_data: participants
});
然后表格应该是这样的:
<table>
<thead>
<tr>
<th>user_id</th>
<th>First name</th>
<th>Last name</th>
<th>Username</th>
<th>total messages</th>
<th>text</th>
<th>images</th>
</tr>
</thead>
<tbody>
<tr>
{% for data in user_data %}
<td>{{data.id}}</td>
<td>{{data.first_name}}</td>
...
...
</tr>
{% endfor %}
</tbody>
</table>