模板引擎将我的代码设置到错误的位置。怎么了?
Template engine sets my code to the wrong place. What's a mistake?
模板引擎将我的代码设置到 table header 之前的错误位置。它成功地设置了简单的字符串 object 但它设置了我的 table 行绝对错误。
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>
link 的输出截图:https://jonarhipov.neocities.org/screenshots/Screenshot%202019-06-26%20at%2015.57.57.png
哪里出错了,还是bug?谢谢!
P.S.: 渲染 table 的最佳方式是什么?我要:
- 按列排序
- 渲染所有数据的一部分。
我不想添加行、删除行、更改行。仅输出 table。
首先,将变量中的所有集合数据发送到html页面,如:
res.render("main.hbs", {
title: "tg:analitycs",
captition: "Таблица пользователей",
cap: "",
user_data: participants
});
那么table应该是这样的:
<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>
模板引擎将我的代码设置到 table header 之前的错误位置。它成功地设置了简单的字符串 object 但它设置了我的 table 行绝对错误。
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>
link 的输出截图:https://jonarhipov.neocities.org/screenshots/Screenshot%202019-06-26%20at%2015.57.57.png
哪里出错了,还是bug?谢谢!
P.S.: 渲染 table 的最佳方式是什么?我要:
- 按列排序
- 渲染所有数据的一部分。
我不想添加行、删除行、更改行。仅输出 table。
首先,将变量中的所有集合数据发送到html页面,如:
res.render("main.hbs", {
title: "tg:analitycs",
captition: "Таблица пользователей",
cap: "",
user_data: participants
});
那么table应该是这样的:
<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>