通过删除按钮将动态表行数据传递给模态对话框

问题描述 投票:1回答:2

上下文 我有一个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)]
node.js bootstrap-modal ejs
2个回答
0
投票

你已经将模态放在forEach循环中。所以你没有一个模态,而是每行有一个模态。这里的问题是你为所有人分配了相同的id removeAccountModal。尝试为每个模态分配唯一的ID,例如:

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

并在每个删除按钮上替换数据目标

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

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


0
投票

谢谢@dimitris tseggenes指出我正确的方向。 每个模态div的独特ids在这里势在必行。事实证明,id属性值不能仅仅由数字组成。

我重构了idtarget-data属性,分别如下所示:

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

我从这篇文章的回复中了解到以下内容:

  1. 如果您有多个模态,即在表中并使用data-target属性分别更改action元素,请不要忘记使模态div唯一
  2. id属性不能仅包含数字
© www.soinside.com 2019 - 2024. All rights reserved.