简体   繁体   English

节点JS。 Jade / Pug页面未呈现

[英]Node JS. Jade/Pug page is not rendered

I'm trying to refresh table with SQL data by click the tab button but it doesn't work. 我正在尝试通过单击选项卡按钮使用SQL数据刷新表,但是它不起作用。

In app.js: 在app.js中:

app.post('/updateTable', function(req, res) {
  var db = new sqlite3.Database('users.db');

  db.serialize(function() {
    db.all("SELECT * FROM user_info", function(err, rows) {
        res.render('index', { title: 'Student Registration', rows: rows });
    });
  });

  db.close();
});

In index.pug: 在index.pug中:

#tabs-2.box
  table
    thead
      tr#tabTit
        th Name
        th Birthday
        th EduID
        th EduGroup
        th Phone
        th Email
    tbody
      - for (var item in rows)
          tr
            td= rows[item].name
            td= rows[item].date
            td= rows[item].eduID
            td= rows[item].gr
            td= rows[item].phone
            td= rows[item].email

In clien-side JS: 在clien端JS中:

$(document).ready( function() {
$('#tabs, #tabs-2').tabs({
      activate: function(event, ui) {
                $.ajax({
                  type: 'POST',
                  url: '/updateTable'
                });
      }
    });
});

The post-request is succefully handled but "res.render" does not work. 成功处理了后请求,但“ res.render”不起作用。

Where am I screwing up? 我在哪里搞砸?

PS Sorry for my English PS对不起,我的英语

Are you sure that the post request is handled? 您确定发帖请求已得到处理吗? You are declaring the activate function, but never calling it. 您在声明activate函数,但从未调用它。

If that's not the issue, try returning the res.render. 如果这不是问题,请尝试返回res.render。 It becomes: 它成为了:

return res.render('index', { title: 'Student Registration', rows: rows });

Finally! 最后! It works with success-handler in the ajax-request. 它与ajax请求中的成功处理程序一起使用。 There is dynamically refresh table now. 现在有动态刷新表。

Render pug-page 'table' 呈现哈巴狗页面“表格”

db.all("SELECT * FROM user_info", function(err, rows) {
    res.render('table', { rows: rows });
});

Success-handler 成功处理程序

$.ajax({
    type: 'GET',
    url: '/updateTable',
    success: function(data) {
               $('#tabs-2').html(data);
             }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM