Refresh section of a page using EJS and Express

旧时模样 提交于 2019-12-03 10:16:09

It's a bit more complex. You'll need to use ajax for this. EJS is server side templates (as you are using them) so you'll want to use jQuery to make the call and update your already rendered page.

Server Your server will need a route that delivers JSON data. Right now you are rendering the entire page. So:

app.get('/rooms/:id',  function (req, res) {

  // Get the house info from database using the id as req.params.id
  ...

  // Return the data
  res.json({
    rooms: 2
    ...
  });
});

Client

Using jQuery make a call to your json route once the user selects the house.

function selectedHouse(house)
{
   if(house.checked){
      // Pass some identifier to use for your database
      $.ajax({
        type: 'GET',
        url: '/rooms/' + house.id,
        success: function(data) {
          // Update the element - easiet is to use EJS to make sure each house has an id/class with the id in it
          // Given an ID of 2 this says find the div with class house_2 and updates its div with class room to the number of rooms
          $('.house_' + house.id + ' .rooms').text(data.rooms);  
      });
   }
}

This is more of pseudo code then anything but should put you on the right track.

trquoccuong

res.render cant rerender view, for refresh page in second time you need use javascript to replace html .This is not good solution

$.get("/",function(html){

 document.open();

document.write(html);

document.close();

});

For better you should use another router to render DOM you want to change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!