Home > OS >  How can I add element from my express server to my html table?
How can I add element from my express server to my html table?

Time:11-15

I want to add an element from server.js using express to my table in my html file. I know you can use double curly braces like "{{example}}" in the html file to import a value from the server file using res.render(views, options). But I wonder if there is a way to add a new row to a table like this :

index.html :

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

</table>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

which add a new row everytime I call my res.render() method. Here what I have in my server file :

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]})
    })
})

CodePudding user response:

Express is a backend server which can set the value in the page to be pushed to front end. To add new rows to a rendered html at Front End , please use a combination of AJAX / Javascript

CodePudding user response:

The ejs templating engine supports Javascript statements like the cursor.forEach in the template. Look, for example, at the <ul> example on the npm page.

  • Related