【问题标题】:How can I add element from my express server to my html table?如何将快速服务器中的元素添加到我的 html 表中?
【发布时间】:2021-12-25 23:58:48
【问题描述】:

我想使用 express 将 server.js 中的一个元素添加到我的 html 文件中的表中。我知道您可以在 html 文件中使用像“{{example}}”这样的双花括号来使用res.render(views, options) 从服务器文件中导入一个值。但我想知道是否有办法向这样的表添加新行:

index.html:

<table>
    <tr>
        <th>Description</th>
        <th>Adress</th>
    </tr>
    <tr>
        <td>{{description}}</td>
        <td>{{adress}}</td>
    </tr>

</table>

每次我调用我的res.render() 方法时都会添加一个新行。这是我的服务器文件中的内容:

server.js:

app.get('/index', (req, res) => {
    var values = []
    var cursor = dbo.collection('collection').find()
    cursor.forEach(function (doc, err) {
        if (err) throw err;
        values.push(doc.name);
        values.push(doc.adress)
    }, function () {
        res.render('index', {description: values[0], adress: values[1]})
    })
})

【问题讨论】:

    标签: javascript html express express-session


    【解决方案1】:

    Express 是一个后端服务器,可以设置页面中的值被推送到前端。 要在前端向呈现的 html 添加新行,请使用 AJAX / Javascript 的组合

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    ejs 模板引擎支持 Javascript 语句,例如模板中的 cursor.forEach。例如,查看npm page 上的&lt;ul&gt; 示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 2022-11-12
      • 2017-03-04
      • 1970-01-01
      相关资源
      最近更新 更多