【问题标题】:Pass Dynamic Table Row Data to Modal Dialog via Delete Button通过删除按钮将动态表行数据传递给模态对话框
【发布时间】:2019-07-23 20:05:30
【问题描述】:

上下文
我有一个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 模式,确认用户确实想要删除该行的数据。在模态中提交后,这将以form 元素的形式发起DELETE 请求。

我想传递所选行的关联account._id 变量,以便我可以将formaction 属性修改为/&lt;%= account._id %&gt;/?_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)]

【问题讨论】:

  • 你为什么不把id像这样action="/&lt;%= account._id %&gt;/
  • 无论你选择哪个删除按钮行,都会取第一行的数据。我需要一种将特定行的帐户 ID 值传递给操作属性的方法。

标签: node.js bootstrap-modal ejs


【解决方案1】:

感谢@dimitris tseggenes 为我指明了正确的方向。
每个模态 div 的唯一 ids 在这里是必不可少的。事实证明,id 属性值不能仅由数字组成。

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

id="account&lt;%= account._id %&gt;id"
data-target=#account"&lt;%= account._id %&gt;id"

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

  1. 如果您将有多个模态框(即在一个表格中)并使用 data-target 属性分别更改操作元素,请不要忘记使模态框 div 唯一
  2. id 属性不能只包含数字

【讨论】:

    【解决方案2】:

    您已将模式放置在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 %>"
    

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

    【讨论】:

    • 我已经进行了建议的编辑并收到此错误:util.js:85 Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#5c7814646759394351bee3f8' is not a valid selector. 该错误是指启动模式的删除按钮:&lt;button type="button" class="btn btn-danger btn-rounded btn-sm my-0 remove" data-toggle="modal" data-target="#&lt;%= account._id %&gt;"&gt;Remove&lt;/button&gt;
    猜你喜欢
    • 2016-01-25
    • 2019-08-19
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    相关资源
    最近更新 更多