node.js - 通过删除按钮将动态表行数据传递到模态对话框

标签 node.js bootstrap-modal ejs

上下文
我有一个GET加载 .ejs 文件的路由,该文件根据传递的变量 accounts 的长度将数据行添加到表中。这里accounts是一个字典数组,键为 _id, type, nickname, rewards, balance, customer_id

    <% if (accounts.length > 0) { %>
      <% accounts.forEach(account => { %>
        <tr class="blue-grey lighten-3 account-row">
          <th scope="row" class="account-id"><%= account._id %></th>
          <td><%= account.type %></td>
          <td><%= account.nickname %></td>
          <td><%= account.rewards %></td>
          <td>$<%= account.balance %></td>
          <td><%= account.customer_id %></td>
          <td>
            <span class="table-remove">
              <button type="button" class="btn btn-danger btn-rounded btn-sm my-0 remove" data-toggle="modal" data-target="#removeAccountModal">
                Remove
              </button>
            </span>
          </td>
          <div class="modal fade" id="removeAccountModal" tabindex="-1" role="dialog" aria-labelledby="removeAccountModalLabel"
            aria-hidden="true">
            <div class="modal-dialog" role="document">
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title" id="removeAccountModalLabel">You are about to remove this account</h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>
                <div class="modal-body">
                  CAUTION!!!
                  <br>
                  <br>
                  Removing this account will delete all the data associated with it. You must visit a local branch to retrieve any monies left residing in the account.
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn grey darken-1" data-dismiss="modal">Cancel</button>
                  <form id="remove-form" action="/" method="POST">
                    <button id="removeAccount" class="btn btn-primary" type="submit">Remove Account</button>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </tr>
      <% }) %>

问题
每行数据上都有一个删除按钮,用于打开 Bootstrap 模式,确认用户确实想要删除该行的数据。在模式中提交后,这将启动 DELETE请求的形式为form元素。

我想传递所选行的关联 account._id变量,以便我可以修改 formaction属性为/<%= account._id %>/?_METHOD=DELETE .

我在访问模式的父窗口 DOM 元素时遇到问题。

我的jQuery代码如下:

<script>
 $(document).ready(function () {
  console.log("loaded and ready to go!"); // works as expected
  var $remove = $(".remove"); // works as expected
  console.log($remove.length); // works as expected
  $(".remove").click(() => {
   console.log("I'm in the modal!") // works as expected
   var $parent = $(this).parent().closest("tr"); // find row
   console.log($parent); // w.fn.init [prevObject: w.fn.init(0)]
   var $account_id = $parent.find("th"); // find account id
   console.log($account_id); // w.fn.init [prevObject: w.fn.init(0)]
   console.log($account_id.text()); // returns blank type string
   $("#remove-form").prop("action", "/" + $account_id + "?
    _method=DELETE"); // change action attribute to account id value
   })
 });
</script>

尝试的方法:

console.log(window.parent.$(this).closest("tr"));       
// returns w.fn.init [prevObject: w.fn.init(1)]

最佳答案

您已将模态框放置在 forEach 内环形。所以你不只有一个模态,而是每一行都有一个。这里的问题是您分配了相同的 id removeAccountModal对于他们所有人。尝试为每个模态分配一个唯一的 id,例如:

<div class="modal fade" id="<%= account._id %>" tabindex="-1" role="dialog" aria-labelledby="removeAccountModalLabel" aria-hidden="true">

并在每个删除按钮上将 data-target 替换为

data-target="#<%= account._id %>"

所以你有独特的模态,现在你可以放置 <%= account._id %>直接在操作属性中

关于node.js - 通过删除按钮将动态表行数据传递到模态对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54950573/

相关文章:

node.js - Mongoose /keystonejs : unique field except if field is empty

javascript - 如何在复选框的 Bootstrap 中禁用按钮?

javascript - 在 Javascript 逻辑中访问 EJS 变量

javascript - 两个事件监听器之间 : How do I pass my function without using trigger()?

javascript - 使用循环的 Node js 和 ejs

javascript - 即使正确渲染,EJS 变量也会引发错误

node.js - 如何进行行搜索?

Javascript 数组测试在为 True 时表现为 False

node.js - NodeJS模块安装

javascript - TypeError : $(. ..).modalForm 不是函数