简体   繁体   English

通过Javascript调用“ POST”路由,这样我就可以更新数据库中的记录(使用node.js,express框架,PUG,mongodb)

[英]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.

相关问题 使用express和node.js更新mongodb中的单个记录 - Update single record in mongodb using express and node.js 使用Node JS Express和PUG(JADE)更新Mongodb - Update Mongodb with node js express and PUG(JADE) node.js(express框架):要求和发布 - node.js (express framework): require and post Node.js Express.js MongoDB:route.post()需要回调函数,但有一个[object Undefined] - Node.js Express.js MongoDB: route.post() requires callback functions but got a [object Undefined] 在Node.js/Express中等待后端数据库更新后如何更新前端? - How can I update the frontend after waiting for the database in the backend to update in Node.js/Express? 在mongoDB中插入记录,然后更新它-Node.Js - Insert record in mongoDB then update it - Node.Js Want to call an api to get the data from mongodb in json format but getting in raw format, using javascript, node.js, express, mongodb - Want to call an api to get the data from mongodb in json format but getting in raw format, using javascript, node.js, express, mongodb 我想使用 node.js 和 mongodb 中的 findOneAndUpdate 更新我的记录。 它更新所有记录期望全名 - i want to update my record using findOneAndUpdate in node.js and mongodb. it update all record expect fullname 如何使用 Express (node.js) 制作一次性路线? - How can i make a one time route using Express (node.js)? 如何使用node.js express中的bodyParser通过AJAX正确发送对象? - How can I properly send Objects via AJAX using bodyParser in node.js express?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM