[英]Call 'POST' route via Javascript so I can update record in database (using node.js, express framework, PUG, mongodb)
I'm trying to update a record in my database when a user decides to edit a table. 当用户决定编辑表时,我正在尝试更新数据库中的记录。 I use javascript to make the table editable row by row and I change the button from 'Edit' to 'Save' image of table before a user can edit and image of table after a user clicks edit .
我使用javascript使表格可一行一行地进行编辑,并且在用户可以编辑之前将按钮从“编辑”更改为“保存” 表格 图像,在用户单击edit之后将其更改为表格图像 。
Once a user clicks'Save', I want to be able to call the route that will find the old record, and overwrite with the new record. 用户单击“保存”后,我希望能够调用将找到旧记录的路由,并用新记录覆盖。 I'm having troubles calling the route in my javascript.
我在用JavaScript调用路由时遇到麻烦。 I've tried to use "location.href = '/people/updateshift'" but it doesn't because it load the people/update shift page, and I just want to be able to call the route in the background without sending a user to the page.
我尝试使用“ location.href ='/ people / updateshift'”,但这不是因为它加载了people / update shift页面,我只想能够在后台调用路由而不发送用户到页面。 Any resources, or pointers would be appreciated :)
任何资源或指针,将不胜感激:)
Thank you in advance! 先感谢您!
function editRow(obj) {
var currentTD = obj.parentNode.parentNode.rowIndex;
var table = document.getElementById("myTableData");
var row_length = document.getElementById("myTableData").rows[currentTD].cells.length;
console.log(obj.parentNode);
//the below is a way to get the HMTL within a cell
console.log(document.getElementById("myTableData").rows[currentTD].cells[0].innerHTML);
if (obj.value == 'Edit') {
document.getElementById("myTableData").rows[currentTD].cells[0].setAttribute("contentEditable", true);
document.getElementById("myTableData").rows[currentTD].cells[1].setAttribute("contentEditable", true);
document.getElementById("myTableData").rows[currentTD].cells[2].setAttribute("contentEditable", true);
document.getElementById("myTableData").rows[currentTD].cells[3].setAttribute("contentEditable", true);
document.getElementById("myTableData").rows[currentTD].cells[4].setAttribute("contentEditable", true);
document.getElementById("myTableData").rows[currentTD].cells[5].setAttribute("contentEditable", true);
document.getElementById("myTableData").rows[currentTD].style.backgroundColor = "#A4D9F5";
var_employeetype = document.getElementById("myTableData").rows[currentTD].cells[0].innerHTML
var_daysworked = document.getElementById("myTableData").rows[currentTD].cells[1].innerHTML
var_num_employees = document.getElementById("myTableData").rows[currentTD].cells[2].innerHTML
var_shift_start = document.getElementById("myTableData").rows[currentTD].cells[3].innerHTML
var_shift_start = document.getElementById("myTableData").rows[currentTD].cells[4].innerHTML
var_shift_end = document.getElementById("myTableData").rows[currentTD].cells[5].innerHTML
console.log(var_employeetype)
obj.value = 'Save'
} else {
document.getElementById("myTableData").rows[currentTD].cells[0].setAttribute("contentEditable", false);
document.getElementById("myTableData").rows[currentTD].cells[1].setAttribute("contentEditable", false);
document.getElementById("myTableData").rows[currentTD].cells[2].setAttribute("contentEditable", false);
document.getElementById("myTableData").rows[currentTD].cells[3].setAttribute("contentEditable", false);
document.getElementById("myTableData").rows[currentTD].cells[4].setAttribute("contentEditable", false);
document.getElementById("myTableData").rows[currentTD].cells[5].setAttribute("contentEditable", false);
document.getElementById("myTableData").rows[currentTD].style.backgroundColor = "#FFFFFF"
obj.value = 'Edit'
location.href = '/people/updateshift'
}
}
You want to look into the jQuery "post" feature. 您想研究jQuery的“发布”功能。 https://api.jquery.com/jquery.post/
https://api.jquery.com/jquery.post/
When the user clicks save, send the value to the server using $.post, and an identifier for which "cell" it is (I don't know how you are doing this), and then have your server deal with updating the record. 当用户单击“保存”时,使用$ .post将该值发送给服务器,并将其标识为“单元格”(不知道您的操作方式),然后让服务器处理更新记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.