我有这种JSON数据。此数据从MongoDB获取。我想在EJS页面上打印它。
[{_id:59157619e9bcd218d9dd4dba, que:'总体来说你对产品的满意程度如何?', 键入:' radio', 选项:['完全不满意','满意','非常满意' ]}]
选项将是单选按钮。
答案 0 :(得分:1)
假设你得到这样的api。
app.get('/testing', function (req, res) {
var array = [{
_id: '59157619e9bcd218d9dd4dba',
que: 'Overall how satisfied are you with the product?',
type: 'radio',
options: ['Not at all satisfied', 'satisfied', 'Very much satisfied ']
}]
res.render('load', array);
//load is the ejs file (load.ejs) and array is the array of object.
});
假设这是你的ejs文件,你想在ejs file.like中发送这个数组...
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
table, td, th {
padding: 8px;
border: 1px solid #ddd;
text-align: left;
}
</style>
</head>
<body>
<h2><%= HeadLine %></h2>
<table>
<tr style='background-color: gainsboro;'>
<th>Id</th>
<th>question</th>
<th>type</th>
<th>options</th>
</tr>
<% array.forEach(function(data) { %>
<tr>
<td >
<p><%= data.seq %></p>
</td >
<td>
<p><%= data.id %></p>
</td>
<td >
<p> <%= data.question %></p>
</td >
<td >
<p><%= data.type %></p>
</td >
<td >
<p> <%= data.options %></p>
</td >
</tr>
<% }); %>
</table>
</body>
</html>
答案 1 :(得分:0)
您需要将json数据从路由器文件传递到ejs文件,这可能有助于 - 的 router.js 强>
router.get('/radio', function(req, res) {
var data = [ { _id: 59157619e9bcd218d9dd4dba, que: 'Overall how satisfied are you with the product?', type: 'radio', options: [ 'Not at all satisfied', 'satisfied', 'Very much satisfied ' ] } ]; //replace this with the service getting data
res.render('radio/show', data);
})
ejs file - radio.js
<form>
<input type="radio" checked><%= data.options[0] %><br>
<input type="radio"><%= data.options[1] %><br>
<input type="radio"><%= data.options[2] %><br>
</form>