I have a EJS template which takes a file userData from the server.
How can I implement the refresh of userData in the template without reloading the page?
I have managed to submit data to the server without reloading the page by Ajax post methods, in other parts of the app.
But can not find an approach to render updated data in the template without reloading the page.
EJS template
It is rendered when browser get to URL /tasks
This snippet is only a part of the app to demonstrate the structure.
<%userData.forEach(function(project){%>
<div class="project">
<div class="projectHeader">
<div class="projectTitle">
<span>
<a data-toggle="modal" data-target="#editDeadLineModal">
<i id="setListDeadline" data-id="<%=project.id%>" class="far fa-calendar-alt fa-2x" data-toggle="tooltip" data-placement="top" title="Set Deadline"></i>
</a>
</span>
<%if(project.deadline == null){%>
<h5 class="projectName"><%=project.name%></h5>
<%}else{%>
<% var deadline = project.deadline.toDateString()%>
<h5 data-toggle="tooltip" data-placement="top" title="Deadline <%=deadline%>" style="align-items: center;" class="projectName">
<%=project.name%>
</h5>
<%}%>
</div>
</div>
</div>
I use forEach loop to grab every single project
in the file and render it in <div class="project">
, so the page get populated with as many objects it is in the userData
.
userData
contains:project.id
project.name
project.deadline
For example I have changed the project.name
and submitted through Ajax post method new data to the server.
I know that it is saved - as it is logged "New data saved" after query execution.
Here is my GET method.
Once URL is reached query the DB for all projects
and send them to the template.
app.get("/tasks", async (req, res) => {
const results = await pool.query(`SELECT * FROM projects`);
res.render("tasks", {
userData: results,
})
})