admin管理员组文章数量:1394520
I'm using Node.js as server-side with express and Twitter Bootstrap as front-end. The page has a dialog with a form and a submit button; the form is submitted through Jquery Ajax call for don't reload the page after the response from node.js server. Here are three steps would like I to do:
page.ejs
<table id="tblProgs" class="table table-bordered table-hover table-striped dataTable">
<thead>
<tr>
<th>
Column
</th>
</tr>
</thead>
<tbody>
<% datas.forEach(function(data) { %>
<tr>
<th width="15%">
<%- data._column %>
</th>
</tr>
<% }) %>
</tbody>
</table>
Ajax content that also is in page.ejs body
$('#formId').submit(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
data: JSON.stringify(data),
cache: false,
contentType: 'application/json',
datatype: "json",
url: '/route/to/nodejs',
success: function (result) {
console.log("Here are the response in json format: " + result);
}
});
});
The url /route/to/nodejs
call this function:
Function called in node.js server:
query = "SELECT * FROM table_name";
sequelize.query(query, {
type: sequelize.QueryTypes.SELECT
}).success(function (result) {
var response = {
data : result,
}
res.send(response);
}).catch(function (error) {
res.render('server-error', {
user: user,
session: req.session,
error: error
});
});
My question is: how to refresh table data at page.ejs in success ajax callback without reload the page?
Thanks!
I'm using Node.js as server-side with express and Twitter Bootstrap as front-end. The page has a dialog with a form and a submit button; the form is submitted through Jquery Ajax call for don't reload the page after the response from node.js server. Here are three steps would like I to do:
page.ejs
<table id="tblProgs" class="table table-bordered table-hover table-striped dataTable">
<thead>
<tr>
<th>
Column
</th>
</tr>
</thead>
<tbody>
<% datas.forEach(function(data) { %>
<tr>
<th width="15%">
<%- data._column %>
</th>
</tr>
<% }) %>
</tbody>
</table>
Ajax content that also is in page.ejs body
$('#formId').submit(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
data: JSON.stringify(data),
cache: false,
contentType: 'application/json',
datatype: "json",
url: '/route/to/nodejs',
success: function (result) {
console.log("Here are the response in json format: " + result);
}
});
});
The url /route/to/nodejs
call this function:
Function called in node.js server:
query = "SELECT * FROM table_name";
sequelize.query(query, {
type: sequelize.QueryTypes.SELECT
}).success(function (result) {
var response = {
data : result,
}
res.send(response);
}).catch(function (error) {
res.render('server-error', {
user: user,
session: req.session,
error: error
});
});
My question is: how to refresh table data at page.ejs in success ajax callback without reload the page?
Thanks!
Share Improve this question edited Jun 24, 2015 at 12:12 BrTkCa asked Jun 24, 2015 at 11:54 BrTkCaBrTkCa 4,7934 gold badges28 silver badges45 bronze badges1 Answer
Reset to default 7You could remove the existing tbody content, and then re-render using jQuery in the jQuery AJAX callback. Something like..
success: function (result) {
$('tbody').empty();
for (var data in result) {
$('tbody').append('<tr><th width="15%">'+result[data]._column+'</th></tr>');
}
})
本文标签: javascriptHow to refresh table data using AjaxJson and NodejsStack Overflow
版权声明:本文标题:javascript - How to refresh table data using Ajax, Json and Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744101102a2590875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论